using foldLeft instead of scan