Complete JS refactoring

Also fixes #186