Some performance enhancements and bugfixes