Bugfixes and small performance improvements