Some refactoring & tests.