Change writer.append to append
Create function writeToFile