improved file opening directory finding logic