Refactored upload logic