Bugfixes and improvements from master