Bug fixes and improvements