refactor binary search