Conditions not evaluating as expected