refactoring thread handling method