rewrote JS to be more cross-browser/robust

fixes issue #63