used a more efficient flattenArray()