Conditional not working as expected