Fixed isExternal and local resolution
corrected loop order
Changed resolve duplicates logic