fixed compilation on Windows and tweaked code.