Lots of improvements and optimizations