improve JS for gallery