Refactored the code so that the upload functionality is common for both save and upload.