Binary search implementation for lib.
Fix a couple bugs in binary search.