Refactored code that builds API URL