minimizing call stack through (flatten)