Fix binary search infinite loop, and clean up mouse evnt dispatching.