Hand-transfer of PR #162. Too many changes between now and then to merge...

... automatically.