added option to stop at threshold