Fix files writing bug