Several small bug fixes