rewrote JS to be more cross-browser/robust