Refactor upload code.