Writing buffer now takes into account the size of the read buffer.

This looks to be working for me.