extract common code for handlers