This entire block of code smells like DRY ("Don't Repeat Yourself").