Some performance enhancements and minor fixes