Refactor leave handler