refactoring some JS