Added flatten function.