optimized grid.js to run faster