Neaten up the cancelling code within the worker.

It's now more obvious what is happening and we can call the code from elsewhere
as well.