Various bugfixes and improvements.