Implement tests. Refactoring