Refactoring image code