Fixed file writing bug