Refactored code for upload