Refactor spawn code a little. Fixes #4535

This is still not optimal and will need to be addressed in the future.
We plan to refactor the redirect API anyway though, so this will be
addressed then.