writing to a file moved to the function