rewrite of recovery method: use simpler depth-first search
remove branch from loop