modified write_to_file 3