uses fold instead of foreach