small refactorings and add some tests