Fix infinite looping issue