More refactoring and fixes.