The manual describes using parenthesis in IF/ THEN statements as so:

IF (X) AND (Y) THEN
IF (X) OR (Y) THEN

This forces evaluation of the conditions prior to making the comparison. It does not appear, from the code posted, that you are doing so.