Refactor all file operations in synchronize_file method.