You are correct. I had a working algorithm in the issue, but yours is better. I have an older incorrect version in this pull request.