some refactoring on both client and server