refactoring code and protocol