change writing data into a function