I have edited your post to add code tags. With this, I saw that it seems to have some syntax error around line [75-84]. I'm not saying it is the reason why you have this error but you have to fix it.
Moreover, to reduce bugs opportunities, why don't you store
one time for all?
Fix these remarks and let us know if issues are still here.