very ugly but working version of binary search