Fix some bugs under Windows. Small refactoring of code