Fixed

Line 35 was while(j<i), it should have been while(j<gridsize)