a working code agent
code refactor to remove repetition