Indentation fix
Seperated file handling and writing.
Removed binary mode for windows compatibility.
Added the "with" statement for automatic file closure.