added lines to save data at each step