Working version with tests.