refactor and simplify binary search for sampling