Flag checking was not done properly in do_write. Corrected it