Added check for a gamestate which is terminal. Plus done some refactoring.