Optimizations and more unit tests