More JS refactoring