js rewrite in js. also closes #1 and closes #2