fix stack overflow errors