Better cleanup of dead processes.

This should fix #1785. Still testing it tho ...