Js refactor

finally feel ok about merging to master.