Optimizations and minor tweaks