Finishing thread execution correctly