This functionality was implemented twice, in two different ways. This commit factors it out into a helper method that can be reused in the two places. We chose to go with the more explicit implementation because it’s easier to understand.