refactoring and updating to using threads