Refactored binarySearch and added test cases