inner loops fixed (hopefully...)