wrong ending condition on loop