refactoring trap handling