See the following FAQ, I think it will answer your question about it "running twice".

How to use UDFs -
http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=81943#Post81943