refactored checkwin function