Moved repeated code into a subroutine.