Improved loop detection in problem solver