MinGW fix

These patches fix and improve compilation with mingw-w64.