better type inference
fix binary search logic