Various bugfixes and enhancements