try replacing recursion with loop
figure out recursion when empty arrays