moved write file function