fixed enconding problems when writing to file