Refactored image upload