Why is my condition not working properly?