This code is a perfect demonstration of why problem code should be posted with a description of what the poster thinks the code is doing ... a description of the process, not the code.
added:
And the first thing you could do is check the matching Parenthesis
added:
Which version of AutoCAD are you using?