Fix possible hang if worker holding the stop flag was killed while holding it. Lots of refactoring / cleanup. View commits