Refine lifetime of parameters to functions

Fixes crash caused when making function calls in a global context, and
probably fixes other things.