Refactor Code

Extract the code to query data from API into a separate function.