Optimizations and Bug fixes