David's additions for thresholding