somewhat major restructuring to allow the use of a threadpool