tidied up AI routine slightly at cost of creating another messy function