Graininess can occur if the sugar crystallizes, which can happen if the mixture is stirred too vigorously while cooling. To avoid this, mix until just combined and then allow it to set without disturbance.