define(function (require) {

  var React = require('react'),
      stores = require('stores'),
      moment = require('moment'),
      CryptoJS = require('crypto'),
      Gravatar = require('components/common/Gravatar.react'),
      Router = require('react-router');

  return React.createClass({

    getInitialState: function () {
      return {
        isLoadingMoreResults: false,
        nextUrl: null
      }
    },

    updateState: function() {
      var instanceHistories = stores.InstanceHistoryStore.getAll(),
          state = {};

      if(instanceHistories && instanceHistories.meta.next !== this.state.nextUrl){
        state.isLoadingMoreResults = false;
        state.nextUrl = null;
      }

      if (this.isMounted()) this.setState(state);
    },

    componentDidMount: function () {
      stores.InstanceHistoryStore.addChangeListener(this.updateState);
    },

    componentWillUnmount: function() {
      stores.InstanceHistoryStore.removeChangeListener(this.updateState);
    },

    onLoadMoreInstanceHistory: function(){
      var instanceHistories = stores.InstanceHistoryStore.getAll();

      this.setState({
        isLoadingMoreResults: true,
        nextUrl: instanceHistories.meta.next
      });
      stores.InstanceHistoryStore.fetchMore();
    },

    renderTitle: function(){
      var instanceHistories = stores.InstanceHistoryStore.getAll(),
          title = "Instance History",
          historyCount;

      if(instanceHistories) {
        historyCount = " (" + instanceHistories.meta.count + " instances launched)";
        title += historyCount;
      }

      return title;
    },

    renderLoadMoreHistoryButton: function(instanceHistories){
      // Load more instances from history
      var buttonStyle = {
            margin: "auto",
            display: "block"
          },
          loadingStyle= {
            margin: "0px auto"
          },
          moreHistoryButton = null;

      if(instanceHistories.meta.next){
        if(this.state.isLoadingMoreResults){
          moreHistoryButton = (
            <div style={loadingStyle} className="loading"></div>
          );
        }else {
          moreHistoryButton = (
            <button style={buttonStyle} className="btn btn-default" onClick={this.onLoadMoreInstanceHistory}>
              Show More History
            </button>
          );
        }
      }

      return moreHistoryButton;
    },

    renderBody: function () {
      var instanceHistories = stores.InstanceHistoryStore.getAll(),
          instances = stores.InstanceStore.fetchWhere({'archived': 'true'}),
          providers = stores.ProviderStore.getAll(),
          instanceHistoryItems;

      if(!instanceHistories || !instances || !providers) return <div className="loading"></div>;

      instanceHistoryItems = instanceHistories.map(function (instance) {
        var instanceId = instance.get('instance').id,
            instanceImage = stores.InstanceStore.get(instanceId).get('image'),
            providerId = stores.InstanceStore.get(instanceId).get('provider'),
            imageId = instanceImage.id,
            provider = stores.ProviderStore.get(providerId).get('name');

        var startDate = instance.get('start_date'),
            endDate = instance.get('end_date'),
            formattedStartDate = startDate.format("MMM DD, YYYY"),
            formattedEndDate = endDate.format("MMM DD, YYYY"),
            now = moment(),
            timeSpan = now.diff(startDate, "days"),
            instanceHistoryHash = CryptoJS.MD5((instance.id || instance.cid).toString()).toString(),
            iconSize = 63,
            type = stores.ProfileStore.get().get('icon_set'),
            image = imageId ? stores.ImageStore.get(imageId) : null,
            imageName = image ? image.get('name') : "[image no longer exists]",
            imageLink;

        if(!endDate.isValid()) formattedEndDate = "Present";

        if(image){
          imageLink = (
            <Router.Link to="image-details" params={{imageId: image.id}}>
              {imageName}
            </Router.Link>
          )
        }else{
          imageLink = (
            <strong>{imageName}</strong>
          )
        }

        return (
          <div key={instance.id}>
            <div className="instance-history">
              <ul>
                <li>
                  <div>
                    <Gravatar hash={instanceHistoryHash} size={iconSize} type={type}/>
                    <div className="instance-history-details">
                      <strong className="name">{instance.get('name')}</strong>
                      <div>Launched from {imageLink}</div>
                      <div>{"Ran: " + formattedStartDate + " - " + formattedEndDate}</div>
                    </div>
                    <span className="launch-info">
                      <strong>{timeSpan + " days ago"}</strong>
                      {" on " + provider}
                    </span>
                  </div>
                </li>
              </ul>
            </div>
          </div>
        );
      }.bind(this));

      return (
        <div>
          {instanceHistoryItems}
          {this.renderLoadMoreHistoryButton(instanceHistories)}
        </div>
      );
    },

    render: function () {
      return (
        <div>
          <h2>
            {this.renderTitle()}
          </h2>
          {this.renderBody()}
        </div>
      );
    }

  });

});

@@ -2842,32 +2842,120 @@
 ) %7B%0A        var 
+providerId = null,%0A            imageId = null,%0A            provider = null,%0A            
 instanceId = ins
@@ -2992,24 +2992,25 @@
         
+_
 instance
 Image = 
@@ -3005,14 +3005,8 @@
 ance
-Image 
 = st
@@ -3035,24 +3035,91 @@
 (instanceId)
+;%0A        if(_instance) %7B%0A            var instanceImage = _instance
 .get('image'
@@ -3119,17 +3119,17 @@
 'image')
-,
+;
 %0A       
@@ -3150,41 +3150,17 @@
 d = 
-stores.InstanceStore.get(
+_
 instance
 Id).
@@ -3155,19 +3155,16 @@
 instance
-Id)
 .get('pr
@@ -3171,17 +3171,17 @@
 ovider')
-,
+;
 %0A       
@@ -3211,20 +3211,129 @@
 Image.id
-,%0A  
+;%0A        %7D else %7B%0A            providerId = null;%0A            imageId = null;%0A        %7D%0A        if(providerId) %7B%0A
         
@@ -3344,16 +3344,29 @@
 ovider =
+ providerId ?
  stores.
@@ -3398,21 +3398,113 @@
 rId)
-.get('name');
+ : null;%0A          provider = provider.get('name');%0A        %7D else %7B%0A          provider = null;%0A        %7D
 %0A%0A  

Change InstanceHistory logic to avoid crashing when an Instance cannot be found