Finish adding lookup tests and refactor the lookup code to make it
more maintable.