AI defensive algorithm. Some bugfix.