Fixed off-by-one and unnessary wrapping issue in buffer.