Redoing algorithm