rewrite stuff, add documentation