partiioning out the generate_file function and adding a check if file should be created