Modified to compile on mingw32, and some fixes