significant Javascript Refactoring