Rewrite in JavaScript - closes #1