converted loop to fold operation