fixing write after end