massive refactor for AGREE