I've got another version that has much better comments.  I'll post that in a second after I do some more local testing.