This block introduces a fair amount of duplication with the code block above.

Is there a way to combine this and the code above to make the code more DRY ("Don't Repeat Yourself")?