refactor to move function into threads.c