A complete rewrite of this post.