in reply to subroutine inside subroutine

And what is the question?

The code you posted is broken in multiple dimensions, no idea where to start...

Cheers Rolf