final working code with game little changed