Various improvements and optimizations