Remove duplication

Extracts duplicated code into a single method.