Apply some bug fixes submitted by Dave Lyons.