Lots of small bug fixes