Refactored. Added test.