Improved version with unit-tests.