major javascript refactoring