improved the adaptive thresholding a bit