I thought I posted this, but later I don't see it.
Here's some beta code which demonstrates the idea. It should work in the vast majority of typical cases. I'm aware there are a few odd cases where it will not. I'm working on those. See inline comments.
Comments and suggestions welcome.