refactored dfs_recursive