Reapplied some changes that were dropped in an old merge.