Add documentation

...and some code cleanup