Another attempt to merge Windows fixes from jtanx

This merges some fixes from @jtanx that had conflicts.