fix empty or infinite loop