added write_file function