Changed message handling logic in agent.go.