refactoring to use common code between server and client