Add anon. functions so that defers are executed where expected.