Rewritten in plain js.