Implementation of Backtracking