dfs method improved