Made a modification to DFS so that it should now run in O(N) time