refactor game end detection code. Resolves #12, Fixes #15