Misc bugfixes and improvements