Move repetitive code to two helper functions.