Updated the findAgent() function