fixed building on Windows plus some refactorings and cleanups.