Clean up the JS a bit

This piece of crap really needs a rewrite from scratch...