not writing correct number of bytes