Refactoring fork() and related functions