Move file-writing to functions.