Deep refactor. AI added