WIP: not fully working -- refactor of JS

We didn't get all the way through the second refactor, but here's a start!