Added some code to fix the q function. Needed a global variable for the function