More refactorings and fixes