flatten function working