This is largely Aaron Levin's work. But it was done in a merge commit, so couldn't be cherry-picked in a sensible way. Thanks Aaron!
And import fix.