Excape from doubly nested loop quickly for efficiency