replaced foldr with foldl