Having trouble writing to file