Build functions in a closure, fixing mutability issues