Previous merge commit missed out some commits - only got 17 of #130's 27

Since the code on current PTG master is broken (and fixed by the commits here which make up the rest of PR #130), I am going to merge this myself.