
675 N.W.2d 584 (2004)
Jan R. MLYNARIK, Appellant,
v.
Terri BERGANTZEL, Appellee.
No. 02-1348.
Supreme Court of Iowa.
February 25, 2004.
*585 Randall J. Shanks of Shanks Law Firm, Council Bluffs, for appellant.
Joseph J. Hrvol of Joseph J. Hrvol, P.C., Council Bluffs, for appellee.
TERNUS, Justice.
The plaintiff, Jan Mlynarik, filed suit to recover a contingency fee paid to the defendant, Terri Bergantzel, a nonlawyer, for services rendered by Bergantzel to Mlynarik in the negotiation of a settlement of Mlynarik's personal injury claim. The district court dismissed Mlynarik's suit on the defendant's motion to dismiss, ruling there was no legal basis for the plaintiff to recover fees already paid notwithstanding the fact the contract between the parties was against public policy and unenforceable.
We conclude Mlynarik has pled facts that would permit him to recover the sums paid pursuant to the illegal contract between him and Bergantzel. We also reject Bergantzel's fallback argument that Mlynarik's claim is barred because he failed to assert it as a compulsory counterclaim in prior litigation between these parties. Finding no basis for affirmance of the district court's dismissal of Mlynarik's petition, we reverse the district court's decision and remand for further proceedings.

I. Principles Governing Our Review.

This case was dismissed upon motion made pursuant to Iowa Rule of Civil Procedure 1.421(1)(f), providing for dismissal of a petition that fails to state a *586 claim upon which relief may be granted. We review a district court ruling on such a motion for the correction of errors at law. Albrecht v. Gen. Motors Corp., 648 N.W.2d 87, 89 (Iowa 2002). "`A motion to dismiss is sustainable only when it appears to a certainty that the plaintiff would not be entitled to relief under any state of facts that could be proved in support of the claims asserted.'" Id. (citation omitted). Thus, a dismissal at this stage must rest on legal grounds. Trobaugh v. Sondag, 668 N.W.2d 577, 580 (Iowa 2003). For this reason, motions to dismiss for failure to state a claim upon which relief may be granted are rarely an appropriate vehicle for disposing of actions without trial. Am. Nat'l Bank v. Sivers, 387 N.W.2d 138, 140 (Iowa 1986).
In determining whether the plaintiff has stated a claim upon which relief may be granted, we consider only the facts alleged in the petition or those of which judicial notice may be taken. Tate v. Derifield, 510 N.W.2d 885, 887 (Iowa 1994). But see Grimm v. U.S. West Communications, Inc., 644 N.W.2d 8, 12 (Iowa 2002) (considering facts conceded by the plaintiff). In construing the petition, we view the plaintiff's allegations "in the light most favorable to the plaintiff with doubts resolved in that party's favor." Haupt v. Miller, 514 N.W.2d 905, 911 (Iowa 1994). Similarly, the facts alleged in the petition are accepted as true. Albrecht, 648 N.W.2d at 89.

II. Background Facts and Proceedings.

The petition filed by Mlynarik stated he sought "to recover monies that the defendant charged him for her representation of the plaintiff in a personal injury claim." Mlynarik alleged the following facts in support of his claim.
Mlynarik entered into a written contingency fee agreement with Bergantzel, his sister-in-law, to "negotiate with insurance companies and attorneys" to settle Mlynarik's claims arising out of an automobile accident. Bergantzel, who was not a licensed attorney, located witnesses, prepared affidavits, made phone calls, obtained copies of Mlynarik's medical records, and obtained a physician's opinion letter on his behalf. After Bergantzel negotiated a $100,000 settlement with the tortfeasor's insurance carrier, Mlynarik paid her $12,288.60, the contingency fee required by their agreement.
Subsequent negotiations with Mylnarik's underinsured motorist carrier were not so fruitful, so Mlynarik then retained an attorney who took over the negotiations. This attorney was successful in obtaining an acceptable offer from the UIM carrier and that claim was then settled.
Mlynarik alleged in his petition that Bergantzel acted as an attorney, and that because she engaged in the unauthorized practice of law, she was not entitled to the fee she collected. He sought recovery of the $12,288.60 he had paid to her for her representation in negotiating a settlement with the tortfeasor's insurance company.
Although not alleged in the petition, we take judicial notice of a prior lawsuit arising out of these events that resulted in a published opinion, Bergantzel v. Mlynarik, 619 N.W.2d 309 (Iowa 2000). See In re Estate of Ditz, 255 Iowa 1272, 1281-82, 125 N.W.2d 814, 820 (1964) (taking judicial notice of prior proceeding, including published opinion by this court). In the case giving rise to that appeal, Bergantzel sued Mlynarik for fees she claimed he owed her for her efforts to settle Mlynarik's UIM claim. Bergantzel, 619 N.W.2d at 311. This court held "Bergantzel's negotiation of a settlement of Mlynarik's UIM claim constituted the unauthorized practice of *587 law." Id. at 318. We concluded "[t]he public policy underlying the attorney licensure requirement dictate[d] that the contract obligating Bergantzel to perform the unauthorized acts [was] unenforceable." Id. Therefore, we reversed a judgment that had been entered in favor of Bergantzel. Id. at 318-19.
In the present lawsuit, Bergantzel made an oral motion to dismiss. The district court sustained this motion, noting our prior decision. The court concluded that if Bergantzel's effort to settle Mlynarik's claim against the UIM carrier was the unauthorized practice of law, so were her efforts to settle Mlynarik's claim against the tortfeasor. Notwithstanding this conclusion, the court found "no law supporting [Mlynarik's] right to recovery of the monies already paid on the contract later found to be unenforceable as against public policy." Mlynarik appealed the dismissal of his petition.
III. Recovery of Monies Paid Under Illegal Contract.
In arguing that Mlynarik is not entitled to recover the fees paid to Bergantzel, Bergantzel relies on a long-standing rule against enforcement of an illegal contract. See Rogers v. Webb, 558 N.W.2d 155, 156 (Iowa 1997); Des Moines City Ry. v. Amalgamated Ass'n of Street & Elec. Ry. Employees, 204 Iowa 1195, 1207, 213 N.W. 264, 269 (1927); Allison v. Hess, 28 Iowa 388, 390 (1870). In Allison, this court held "that where a contract is illegal,... the law will not afford affirmative relief to either, but will leave the parties as it found them." 28 Iowa at 390. This rule has its basis in the in pari delicto doctrine:
[W]here parties to an illegal contract are in pari delicto, a court will ordinarily leave them where it finds them, whether the contract is executory or executed, refusing relief to both. Thus, where money has been paid on an illegal agreement, it is a general rule that if the agreement is executed and the parties are in pari delicto, neither of them can recover from the other the money so paid.
17A Am.Jur.2d Contracts § 322, at 324-25 (1991). We have noted the purpose of this doctrine "is to deter future misconduct by denying relief to one whose losses were substantially caused by his own fraud or illegal conduct." Gen. Car & Truck Leasing Sys., Inc. v. Lane & Waterman, 557 N.W.2d 274, 279 (Iowa 1996).
The rule against providing judicial relief to parties in pari delicto with respect to an illegal contract is not, however, without exception. See In re Sylvester's Estate, 195 Iowa 1329, 192 N.W. 442 (1923) (recognizing two exceptions to rule). In Sylvester, the decedent had engaged a law firm to assist her in obtaining a divorce. Id. at 1330-31, 192 N.W. at 442. The firm was paid a percentage of the money and property received by the decedent in the divorce settlement, pursuant to the parties' written fee agreement. Id. at 1331-32, 192 N.W. at 442-43. After the client died, her estate sought to recover this fee. Id. at 1332, 192 N.W. at 443. The trial court denied the requested relief.
In considering the case on appeal, our court held that the contract, which provided for a contingent fee in a divorce action, was "against public policy[,] illegal and void." Id. at 1333, 192 N.W. at 443. In arguing that it should not be required to refund its fee, the law firm relied on the doctrine of in pari delicto. Id. at 1334, 192 N.W. at 444. It claimed that the parties were equally at fault and therefore the court should leave the parties where it found them. Id. We noted there were exceptions to this general rule:
"Even where the contracting parties are in pari delicto, the courts may interfere *588 from motives of public policy. Whenever public policy is considered as advanced by allowing either party to sue for relief against the transaction, then relief is given to him. In pursuance of this principle, and in compliance with the demands of high public policy, equity may aid a party equally guilty with his opponent not only by canceling and ordering the surrender of an executory agreement, but even by setting aside an executed contract ... and decreeing the recovery back of money paid ... in performance of the agreement. The cases in which this limitation may apply and the affirmative relief may thus be granted include the class of contracts which are intrinsically contrary to public policy,contracts in which the illegality itself consists in their opposition to public policy, and any other species of illegal contracts in which, from their particular circumstances, incidental and collateral motives of public policy require relief."
Id. at 1334-35, 192 N.W. at 444 (citation omitted).
We held this exception applied to the contingent fee contract at issue in Sylvester. Id. at 1336, 192 N.W. at 445. We noted the contract was "illegal because public policy demands that such a contract shall not be made." Id. Because the contract was "intrinsically against public policy and [was] illegal for this reason alone," we held the in pari delicto doctrine did not prevent the court from awarding the plaintiff affirmative relief. Id.
We think the same reasoning applies here. The contingent fee agreement between Mlynarik and Bergantzel is unenforceable solely because it is against public policy. Bergantzel, 619 N.W.2d at 318. "[P]ublic policy demands that such a contract shall not be made." Sylvester, 195 Iowa at 1336, 192 N.W. at 445. Thus, like the agreement at issue in Sylvester, the contract here is intrinsically contrary to public policy. In addition, the public policy against the unauthorized practice of law is advanced by allowing the "client" to recover fees paid pursuant to the illegal contract. See generally Gen. Car & Truck Leasing Sys., Inc., 557 N.W.2d at 279 n. 2 (noting court need not apply in pari delicto doctrine if "the court believes the public interest is best served by awarding relief to the wrongdoer"). Therefore, we think this agreement falls within that class of contracts where affirmative relief may be granted in order to advance public policy notwithstanding that the parties are in pari delicto.[1]
IV. Failure to File Counterclaim in Prior Litigation.
Bergantzel claims that even if the in pari delicto doctrine does not preclude recovery, Mlynarik cannot maintain this action because the claim he asserts was a compulsory counterclaim in the first lawsuit between these parties. Bergantzel *589 contends that as a consequence of Mlynarik's failure to assert his claim there, he is barred from asserting it here. Upon our consideration of this issue, we conclude there is no merit to Bergantzel's defense.
Iowa Rule of Civil Procedure 29 provides:
A pleading must contain a counterclaim for every claim then matured, and not the subject of a pending action, held by the pleader against any opposing party and arising out of the transaction or occurrence that is the basis of such opposing party's claim, unless its adjudication would require the presence of indispensable parties of whom jurisdiction cannot be acquired. A final judgment on the merits shall bar such a counterclaim, although not pleaded.
Iowa R. Civ. P. 29 (now renumbered as Iowa Rule of Civil Procedure 1.241). Also highly relevant to the issue before us is Iowa Code section 631.7(4), which states in relevant part:
The rules of civil procedure pertaining to actions, joinder of actions, parties and intervention shall apply to small claims actions, except that rule 29 shall not apply. No counterclaim is necessary to assert an offset arising out of the subject matter of the plaintiff's claim. A counterclaim ... against an existing party is deemed denied and no responsive pleading by such party is required.
Iowa Code § 631.7(4) (2001) (emphasis added).
The prior action between these parties was commenced as a small claims action by Bergantzel. Bergantzel, 619 N.W.2d at 310. Consequently, rule 29 (now rule 1.241) does not apply, including the provision that an unasserted compulsory counterclaim is barred by a final judgment on the merits. Harris v. Jones, 476 N.W.2d 54, 54 (Iowa 1991) (holding rule that compulsory counterclaim is barred by final judgment in first action does not apply to small claims actions, and allowing such a claim to be asserted in a later lawsuit).

V. Conclusion and Disposition.

We hold the in pari delicto doctrine does not preclude Mlynarik's recovery of the fees he paid to Bergantzel under their illegal and unenforceable contract. Nor does his failure to assert this claim in the prior small claims action filed by Bergantzel bar the present suit. The district court erred in ruling that Mlynarik had failed to state a claim upon which relief may be granted. Therefore, we reverse the district court's judgment and remand for further proceedings.
REVERSED AND REMANDED.
All justices concur except WIGGINS, J., who takes no part.
NOTES
[1]  For purposes of our discussion, we have assumed Bergantzel and Mlynarik are in pari delicto. See Black's Law Dictionary 794 (7th ed.1999) (defining "in pari delicto" as "[e]qually at fault"). Due to our conclusion that the general rule barring relief to a party in pari delicto does not apply, whether Bergantzel and Mlynarik are, in fact, in pari delicto is immaterial. Therefore, we do not consider whether the petition contains facts conclusively establishing that Bergantzel and Mlynarik were equally at fault. See Gen. Car & Truck Leasing Sys., Inc., 557 N.W.2d at 279 (stating "the doctrine applies only where the plaintiff was equally or more culpable than the defendant or acted with the same or greater knowledge as to the illegality or wrongfulness of the transaction"); In re Sylvester's Estate, 195 Iowa at 1336, 192 N.W. at 445 (noting exception to in pari delicto rules where "the position of one party is essentially less illegal and blameworthy than that of the other").
