moved api stuff to separate function