Small corrects (imports, etc). Added flatten code