replaced tcl script with C code
fixed ai, eliminated global(read evil) variables