Upstream merge, plus a few enhancements.

See README for details.