Changed so that ws operate in its own thread