Refactoring. Added tests.