Fix mingw32 crossbuild and code cleanup