Extracted writing to file as a method