Refactored image creation logic