Fixed some minor bugs in the agent loop