Added a function for threading