Incorporation into master

tmhedberg has just cherry-picked my changes (and your fix to them) into his master branch. I am sending you a pull request for that because if you want to build upon our changesets you'll likely prefer to start from his cleaned up version rather than sticking to my original branch (which I plan to abandon).