Added a writeToFile function