Tray.ai interview question

Refactor this code in a readable and future-prone way