Thresholding algorithm loop structure done