We have logic like this in several locations. Putting it in a class like @shazow suggested would make it cleaner IMHO.