Remove a bunch of manual iteration

As per #6353

Only covers about a quarter of the places that could be changed, but I thought I should submit this now to prevent it from becoming difficult to merge in the future.