Some refactoring and new tests