It's better to use bfs instead of dfs for this task (optional fix)