fix: better WriteBufferIfNecessary