added more fixes to writing to file