vars i and currentstate are defined within the function, not within for loop blocks