refactoring exercise 5 from Threads