Substantial refactoring with accent on better granularity and testing.