Use function pointers to reduce duplicated code.