Refactored code to make optimal use of boost::thread