refactor some function, tune timing of saving data.