**I posted the working refactored version below, the fixes are in the comments. I hope this helps.
:cool:

*** code removed