The function below seems to be doing this