Refined ring detection logic substantially.

* All new tests now pass.
* Ring pruning logic needs refactoring.