Refactor writing into external write API.