added cap to limit recursive calls