<pre>Too many people have fiddled with this code by now, and it's somewhat inconsistent internally and hard to reason about. It could use a clean rewrite.</pre>