WIP
finish rewrite to pure javascript