implement WriteToFile function