cleaned deduplicated function