refactoring with threads