some heavy refactoring on agreements api