clean up and split into lib and document