Refactoring: Consolidated binary search code

Numerous sections of the code were reimplementing either similar or
identical versions of a simple binary search. These were consolidated to
a single function. This could possibly be moved to util.c in the future.