Separate out api logic