need to (directory-file-name (file-name-directory dir)) to get the
directory without the trailing slash, otherwise it loops