Added thresholding function.