JS refactoring.

I have a truly marvellous commit message for this, which this repository
is too narrow to contain.