remove code duplicate by using function pointers