New implementation of ringbuffer (better performance, more compact code).