refactor server logic