addded dynamic threshold adjustment