Fix infinite looping and crash problem.