AI Logic (Ported, yet to test)