Move some helper methods around
Add some logging, related refactoring.
Fix a number of silly mistakes.