created a generic function for duplicate code