Preventing infinite loop that was freezing Chrome