Addressed encoding issue when writing to file