refactor fork code