Various bugfixes and performance enhancements