Refactor protocal.py