refactor SaveRemoteFile function.