I'd suggest coding in a more organised way from the start.
NOTE: Although I say 'should' a lot in the following lines, it's up to you

These are the personal rules that I try to follow. By doing so, I almost never have nesting errors, and I can see the flow of the program by looking at indentation.
If you are coding an 'IF' for example, the very next line you should type should be 'ELSE' followed by 'ENDIF'. Then go back and fill in the code. Do the same with FOR/NEXT, DO/LOOP, WHILE/ENDWHILE.
When you insert the code, you should also add extra leading spaces, that indent you code further than the surrounding IF/FOR/whatever pair.
This is 'good':
if a>0
while a > 10
` do something
endwhile
else
for i=0 to 10
`do something
next i
endif
This is 'bad':
if a>0
while a > 10
` do something
endwhile
else
for i=0 to 10
`do something
next i
endif
The *only* time that I don't indent in this way is when I insert debug code - that's because by not indenting, it stands out, and I'm going to remove it later anyway.