Fixed AI bug, refactored lots of stuff