Replace foreach with foldl as we need to accumulate errors in State