some refactoring of server flow