Factor out api stuff to make main cleaner