Added writing to a .bat file (instead of stdout)
Other refactoring as well.