Use transacation to wrap this up more safely.

Also reverse the logic so the job gets created first in the code
and then removed. Given that it's now in a transaction this is
a minor point, but it reads more reasonably.