Refacored lookup function a bit