Add documentation and fix some bugs.