Refactorings and better tests