code restructure to reduce duplication