Refactor to idiomatic JavaScript

About 25x faster in Chrome
About 8x faster in Safari
About 6x faster in Firefox
About 4x faster in IE