refactor, add dfs logic