add check around write_all