added security check. changed encodings of states.