Figured out bug with recursion, needed to define i as a local variable rather than global