Move forking code into utility function