added dynamic threshold