New tests and some refactoring