Rework to be a library.
add some docs