Change write_file to pass by reference