several bugs and improvements