General bugfixes and improvements