Completed scripts to make the function work