? Consider adding comments to this function to make it clearer what is going on.