I've seen this same set of if's three times so far - time to pull it out into a well-named function.