Unless you edited this to shorten the post, you need something like an M30 or a GOTO between the the Z1. line and the N12000 line, or it is just going to keep running the "subprogram" as an inline part of the main program. Since in that instance the M99 is not returning anywhere (as it would if there was an M97 or G65 call that sent the program to a subprogram), it will just jump to the head of the program (and loop endlessly).