fixed a bug for small grid sizes
added comments