Code from class on dfs