Nsquare loop seems to work correctly now