I'm fine with a less-than-optimal implementation to start... as long as there are tests.  Also, because you're modifying the `visited` object in the function anyway, just define that variable outside of the closure (as opposed to passing it to each call of the method.