correctly handle partial writes