refactor near duplicate methods into single function that dynamically generates them