setq closest nil before run
To build on this answer I would wrap the entire function in a lambda statement like so:
((lambda ( / ) ; insert local variables here
; insert all of your code here
)) ; close lambda function
The reason I use lambda is I don't need to make a function, however I need to localize my variables. I see that your code evaluates the variable closest in an if statement. When closest is not localized it will run "correctly" the first time, however if it is not set to nil the next time, it will run incorrectly. If you use lambda and put all the variables you use past the "/" the variables will be nil at the start of the program.