Use depth-first search