Fix function closure captures
Use shared code to store global variabels