Refactored code. Repared tests.