Merge of various bugfixes etc

Did this a while ago when I was seeing if we could use this to test one of our main codebases. Didn't prove a good fix for that one so I froze changes but we've recently started looking at it again for new & more well-formed projects. This PR is FYI, although you're welcome to merge; we now have an OS commit-back policy at work so if we do use this in the future we should be able to send updates (& possibly tidy this lot up).