Almost identical code in two places suggests it should be commoned up.