converting this from a script to a function