Refactor the history handling