This block is repeated three times. Please refactor into a separate routine.