Large patch from Ben Allison fixing the MSVC build.

Patch tweaked a little to fix Linux build and clean up minor problems.
10 files changed