DRYing up error-checking logic