moved function to the top of the script