Added the thread functionality to increase the performance