write file correctly