refactor gallery js