Some implementation refactorings and added a missing UT.