WIP: refactoring code and use of ringbuffer