replaced checked by real grid calculation, refactored code