This code needs cleaning in a future submit.

Having a 5 way boolean conditions is rather hard to understanding :)