Simplify by just sending a regular kill to all processes (rather than only those that are active) at the beginning and improve by incorporating a wait call to reap any leftover zombie processes at the end.