Replaced unnecessary groups by distinct. Minor refactoring of lookup_name.