Fix build for Visual Studio 2008/2010. Did not test 2012, if it builds without these changes VC compiler version needs to be added to the ifdefs.
Fixed unix build

Unix build got broken by commit 3f5cbad9d.

I also did a minor refactoring of the code shipped with commit 3f5cbad9d.