General optimizations and improvements