implementd write and read functions