Implement part of dfs.