Fix logic issue in if statement