fixed bug in threshold finding