pulled out common functionality from copyFile and saveFile to new function