AI Code Second Pass

Second pass of AI code. It's still got a few kinks to work out, but I'm
very close to finishing!