Code fixes and improvements