Added check to dfs