Please help me with this. Can’t trace the error. Check comments for entire code.
I just realized that I was missing the flattening step. It solved the problem.