refactored spawning function