Fixes to build on windows and prevent some segfaults.