clean up, optimize binary search