Optimizations and bugfixes galore