General refactorings and unit tests