Implemented write_to_file method