Updated the function and test code