simplifying flatten function