moved api call out of loop and tweaks