Why my conditional logic is not working as it should?