refactored image upload script