Fix wrap around bug in ring buffer.