Factor out global threshold function.