Break out of the loop in the proper place. Thanks @techhat.