Working version with good tests.