Cheddar isn’t a great cheese for melting - it has a tendency to break and turn into a grainy, oily mess. Replacing the cheddar with gruyere will help a lot. Did you use milk, half and half or cream in your previous attempts? Milk alone will tend to break/curdle - half and half or cream (or a combo) works better. Making sure the potatoes are very evenly sliced will make them cook more evenly. I layer mine in thin layers and pour some cream on each layer to make sure that all of the potatoes get thoroughly coated. I also pre-cook the sliced potatoes in some of the cream - it makes them more flexible and easier to layer tightly in the baking dish.