Make recurseModel method tail-recursive, fix stack overflow error