I noticed this pattern repeated a couple of times.

Is it possible to refactor it in a separate function and re-use this function?