Fix bugs in spawning system.

See commit messages and #9776