Rebase on 3.1

Didn't want to force push this rebase - check it over and if it looks ok merge it in. A few conflicts resolved and on the whole want a pretty painless rebase.