fixed exceptions, using foldLeft