I think you might have a better chance of attracting help if you simplify your example. From my point of view, it's too challenging to attempt to make sense of that large blob of text.
Strip it down to show us only the parts which are giving you trouble. Include any error message you receive when your simplified code fails.