More refactoring and corrections