added ai functionality, fixed some bugs