Fix error handling in spawn()