refactored to code for binary search