fixed writeFile function