Refactor JavaScript to readable state