Refactor recursion detector