consolidate multiple instances of copypasted code into a helper function