fixed print_to_file function