Fix several bugs with AI