Improve border correction routine

Use minimum of global and local median diffs to calculate threshold.