reorder definitions, fix a bug, name exports and some other stuff
implement high-level API