doc cleanup and some refactoring