Code fixes and partial MSVC port