Remove the duplication in this and above function. Create a common function that both of these functions can use.