Merge bug fixes from trunk.

FossilOrigin-Name: 1d7b2dc0eae70c0c0e523b715acf758bb4cfa9ac