Lift some common code into a subroutine instead of duplicating it three
times.