Add backtracking function