Refactored. Need to fix tests.