Various small bug fixes and improvements