Refactor: change flatten() to static to be shared