modified write_to_file