fixed doc-test errors and minor refactoring