fixed backtracking algorithm errors