Refactored onAfterClose callback implementation