More refactoring and fixing.