refactors lots of JS stuff, fixes #108 (weird relation to #108)