More refactoring and some fixings