Refactor JavaScript slightly