Converted script to function