I've rearranged this stuff a bit in my latest commit.  Hopefully it won't be a messy merge here.