Fixed Win32 compatibility

Hi, I have applied changes from repo @mainland to the current master. It is practicly the same as original mainland's commit with a few minor tweaks.