Refactor server logic for websockets and sync