updated code for handling threads