code refactoring of images functions