doc and code cleanup
Lots of refactoring and cleanup