Somewhat improved lookup algorithm.