A few bugfixes and improvements