Fixed server code to be threaded correctly