Added backtracking algorithm