Moved some doctests to unittests. Some code polishing.
merge pull 94