Lots of refactorings and updates of units tests