fix bugs in recursive calls