JS refactoring in progress on this one.

Raising a pull request so I can view the diffs and think of ways to refactor better.