Implemented AI, with bugfixes