Cleanup

It could still use some more cleanup, but at least now it passes JSLint
apart from some nonsensical "unused variable" errors.