More refactoring and test code polishing