added new thread functions, fixed some thread issues