Refactoring a bit.
Fix tests.