Fixed file writing issue