refactored binary search code