library rewrite

TODO:
 * documentation
 * tests