Move forking code in separate function.