Improve code structure. Added tests.