fixed alg to calc grid boundaries