big refactoring for more JS-like api