Cleaned up and working write file to sd card