Have you tried typing each command in the lisp to see if the sequence of prompts / inputs is correct? example: -refedt (enter something) (enter something) "next", etc?
Also add some temporary princ statements of variables inside the PROGN, example (princ "\nnestlevel")(princ nextlevel), and see if the values match what is needed for prompts? Or - in Acad, use IDE to walk through each line step by step and use watch window to check variables