#5 preparing general ring buffer code. Not yet tested.