Refactor JavaScript