Some JS refactoring