lots of cleanup and refactoring.