Massive refactor to use state machine for the grid