reimplement in javascript and condense to one file