Tests and refactoring