Refine import function and attchment encoding