halfing algorithm works