Improved function closing