Major overhaul of protocol and state.py