state transition rules
few checking and bug fix