Refactored code interfacing with API so it was easier to read