Refactoring to make crazy if statement a little more understandable