Also, not sure how ugly/incomprehensible it would be, but it would be possible to remove all these if statements and convert this into one big boolean expression. If you spaced it out and added the comments as before, that may be a bit nicer.