Various fixes and WIN32 support