         })
     }
     
-    func handleResponse(data: NSData!, response: NSURLResponse!, error: NSError!) {
+    private func handleResponse(data: NSData!, response: NSURLResponse!, error: NSError!) {
+        if error != nil {
+            showAlertWithError(error)
+            return;
+        }
+        
         var jsonError: NSError?
-        let dictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.allZeros, error: &jsonError) as? Dictionary<String, AnyObject>
-        let results = dictionary?[JSONResultsKey] as? Array<Dictionary<String, AnyObject>>
+        let responseDict = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.allZeros, error: &jsonError) as? Dictionary<String, AnyObject>
         
-        if results != nil {
-            for item in results! {
-                stories.append(StoryModel(item))
+        if jsonError != nil {
+            showAlertWithError(jsonError)
+            return
+        }
+        
+        if let pages = responseDict?[JSONNumPagesKey] as? NSNumber {
+            numPages = pages as! Int
+        }
+        
+        if let results = responseDict?[JSONResultsKey] as? Array<Dictionary<String, AnyObject>> {
+            currentPage++
+
+            for i in results {
+                stories.append(StoryModel(i))
             }
             
             tableView.reloadData()
         }
     }
     
-    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
-        return stories.count
-    }
-    
-    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
-        let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! UITableViewCell
-        let story = stories[indexPath.row]
-        
-        cell.textLabel?.text = story.title
-        cell.detailTextLabel?.text = story.author
-        
-        if useAutosizingCells && tableView.respondsToSelector("layoutMargins") {
-            cell.textLabel?.numberOfLines = 0
-            cell.detailTextLabel?.numberOfLines = 0
-        }
-        
-        return cell
+    private func showAlertWithError(error: NSError!) {
+        let alert = UIAlertView(
+            title: NSLocalizedString("Error fetching data", comment: ""),
+            message: error.localizedDescription,
+            delegate: self,
+            cancelButtonTitle: NSLocalizedString("Dismiss", comment: ""),
+            otherButtonTitles: NSLocalizedString("Retry", comment: "")
+        )
+        alert.show()
     }
 
 }


import UIKit

let useAutosizingCells = true

class TableViewController: UITableViewController, UIAlertViewDelegate {
    

        tableView.infiniteScrollIndicatorMargin = 40
        
        // Add infinite scroll handler
        tableView.addInfiniteScrollWithHandler { [weak self] (scrollView) -> Void in
            let scrollView = scrollView as! UITableView
            
            self?.fetchData() {
                scrollView.finishInfiniteScroll()
