working with FLATTEN, ORDER and nested FOREACH