server code refectored