removing debug code and implementing a break in the loop