Various optimization attempts.

Added depth count to force breadth first search.
Still slow in some bad cases.