Some bugfixes and improvements