Fixed writing to file