Refactor upload code