     
     // Make request
     let session = URLSession.shared
-    let task = session.dataTask(with: urlRequest, completionHandler: {
-      (data, response, error) in
-      // handle response to request
-      // check for error
-      guard error == nil else {
-        completionHandler(nil, error!)
-        return
+    let task = session.dataTask(with: urlRequest, completionHandler: deserializeAndCallCompletion(completionHandler))
+    task.resume()
+  }
+  
+  // Create a completion that does the deserialization
+  private static func deserializeAndCallCompletion<T: Codable>(_ completionHandler: @escaping (T?, Error?) -> Swift.Void) ->
+    (Data?, URLResponse?, Error?) -> Swift.Void {
+      return {
+        (data, response, error) in
+        // handle response to request
+        // check for error
+        guard error == nil else {
+          completionHandler(nil, error!)
+          return
+        }
+        // make sure we got data in the response
+        guard let responseData = data else {
+          print("Error: did not receive data")
+          let error = BackendError.objectSerialization(reason: "No data in response")
+          completionHandler(nil, error)
+          return
+        }
+        
+        // parse the result as JSON
+        // then create a Stationboard object from the JSON
+        let decoder = JSONDecoder()
+        decoder.dateDecodingStrategy = .iso8601
+        
+        do {
+          let connections = try decoder.decode(T.self, from: responseData)
+          completionHandler(connections, nil)
+        } catch {
+          print("error trying to convert data to JSON")
+          print(error)
+          completionHandler(nil, error)
+        }
+        
       }
-      // make sure we got data in the response
-      guard let responseData = data else {
-        print("Error: did not receive data")
-        let error = BackendError.objectSerialization(reason: "No data in connections response")
-        completionHandler(nil, error)
-        return
-      }
-      
-      // parse the result as JSON
-      // then create a Connections object from the JSON
-      let decoder = JSONDecoder()
-      decoder.dateDecodingStrategy = .iso8601
-      
-      do {
-        let connections = try decoder.decode(Connections.self, from: responseData)
-        completionHandler(connections, nil)
-      } catch {
-        print("error trying to convert connections data to JSON")
-        print(error)
-        completionHandler(nil, error)
-      }
-      
-    })
-    task.resume()
-  }
-  
+  }
   
 }
 
 // MARK: Object model
+
+struct Stationboard: Codable {
+  var station: Locations?
+  var stationboard: [Journey]?
+}
 
 struct Locations: Codable {
   var stations: [Location]?

enum BackendError: Error {
  case urlError(reason: String)
  case objectSerialization(reason: String)
}

extension String {

    case categoryCode
    case number
    case company = "operator" // needed since "operator" is a Swift keyword
    case to
    case passList
    case capacity1st
    case capacity2nd
  }
}

