Add auto-close function logic