Lots of optimizations and speedups