Refactored the binary search stuff.