Refactoring code for different protocols