Conditional logic is not working right