added state parameter to several functions (first attempt: removing pnum global)