fixing buffer array reading and writing