Replace the for-next with while-endwhile because you waste a lot of cpu time going through the entire 65535 numbers, when the free space was object number 1 after all. Hehe, didn't I post something like this here before?
Whoops! Sorry, this line:
if object exist(i)=0 then free=i
should be:
if object exist(i)=0 then free=i: exit
Then it will stop when it finds it.