Some code for finding threshold