implementation of circular buffer