Refactoring image function