Fix binary search function, add debug info