Don't unlock lock before removing, move cleanup hook to before mktemp is executed