Major refactoring of agent code in a new structure