Crazy thresholding algorithm