Made grid function correctly