Can this logic be encapsulated in a class or method somewhere? It seems to exist in more than one place in the code base.