updated the function based on Nick's comments