add write funcion,refactoring