Refactored domain spawning in init into a separate function and added error handling.