Trying to fix the backtracking algorithm