Refactored agent for better testability