added circular buffer logic