Fix broken lookup algorithm