Combined thread functionality into one function. Various other changes