corrected the dynamic grid layout function