implemented the threshold algorithm