Refactoring javascript

@JacobKorn @boredsteve