improve codes for threshold