What about breaking this up into methods to improve readability? It took me a while to understand what this was checking.