refactored code for custom ai a bit and added lots of comments