     
 # Downloads an image.
 #
-# @param [Number] id the Pixabay image ID
+# @param [String] id the Pixabay image ID
 # @param [Number] resolution the resolution to download the image in
 # @param [String] destination the path of the destination file
 #
 download = (id, resolution, destination) ->
   # Build URL for API request.
-  url = 'https://pixabay.com/api/?key=' \
-    + user.apiKey \
-    + (if resolution > 1 then '&response_group=high_resolution' else '') \
-    + '&id=' \
-    + id
+  url = buildUrl(user.apiKey, id, resolution)
   
   log.info 'Requesting information for image with ID ' \
     + id \
     + ' from ' \
     + url
     
-  # Request JSON from Pixabay.
+  # Call out to Pixabay.
   options =
     url: url
     json: true
   request options, (error, response, body) ->
-    if !body.hits
-      log.error 'Couldn\'t get information about image ' \
-        + id
+    if error || !body || !body.hits
+      log.error 'Couldn\'t get information about image with ID \'' \
+        + id \
+        + '\''
     else
+      url = body.hits[0][resolutions[resolution]];
+      log.info 'Downloading file from ' \
+        + url
       file = fs.createWriteStream destination
-      req = https.get body.hits[0][resolutions[resolution]], (res) ->
-        res.pipe file
+      req = https.get url, (response) ->
+        response.pipe file
 
       
 # Installs a single image.

]


humanResolutions = [
  'tiny'
  'small'

log = new Log logSettings


# Array of resolution property names.
resolutions = [
  'previewURL'
  'webformatURL'
