Re-commit r130862 with a minor change to avoid an iterator running off the edge in...