made thread function better