Refactored grid. Fixed #158.