refactor gallery script