Upgrade several function loops to 3d iterations