move file writing to method