The code in both places was the same verbatim, so we can extract it to a function to deduplicate the logic.