Fixed circular buffer bug, added tests to verify.