refactor to use vanilla JS; improve cross-browser compatibility