refactoring image logic