Numerous small bug fixes