rewrite to js, closes #33