Also includes a fix for the segfaults on Windows caused by the original version of this patch. This reverts commit 4b51194d.