Added AI and cleaned up code