refactoring gallery.js