A bit of refactoring and tests