flatten script bug fix