Restructure trap/add trap function