This obsoletes id:20161106121501.11062-2-david at tethera.net The actual net diff is the same, except for adding some tests. The changes have been broken up into smaller pieces to make review easier.