moved out defer handler