Further tests and refactoring