Some JS Refactoring