Copied code from google-api