Add documentation and some code cleanups