Closing the writebuffer at the right place