Use local variables in functions (rebased)

This is a rebase of #7 for master.