No worries, I had to stare at it a long time, and test it myself to verify it really is correct.

Oh, I also dropped one line from @marineam 's original version by putting the "then" on the same line :)