Fix circular buffer rollover

The circular buffer code was not correctly rolling over the dend pointer when
the end of the taken area was beyond the start of the buffer.