Code refactoring. Fixed tests.