More refactoring and fix unit tests