Fixed bug in edge of grid checking