Sorry for throwing everything into one commit, splitting my changes to individual will probably take ages.