move spawn code into library