Fix writing on wrong file