Refactoring of lazy gallery script