Lots of bugfixes and enhancements