Various bugfixes and new functionality