The nested if here makes this very hard to read.  Can it be refactored a bit please.