Changed buffer to use fill and read index, hopefully it's correct, can someone check please.