MSVC Fixes

A few fixes for compilation with MSVC. This is all work done by Alex Fuller, I've just cherry picked bits from #370, and done a little bit of tidying.