Did some javascript refactoring