Optimized new code and improved the tests.