Fixed a problem with an unecessary exiting of a loop