Refactoring code for generation of thumbnails.