Refactoring and fix ups.