Ported to plain JavaScript