added trapping functionality