This looks like something that could be extracted to a halper.