Add documentation and clean up code (a bit).