Got write working much better, I have a problem when the buffer needs to wrap around