Code refactored and some tests.