cleanup dupe code into a function