Add tests and corresponding refactoring