added comments and improved AI