Master to dev merge.

I'm going to leave this one up for double-checking, since the merge was kind of dodgy and there were several compile errors I had to fix up afterwards. Hopefully didn't cock anything up too badly.