Changed foldl to foldl' (as experts say never to use foldl)