contract Upgradeable is Ownable{

  address public lastContract;
  address public nextContract;
  bool public isOldVersion;
  bool public allowedToUpgrade;

  /**
   * @dev makes contract upgradeable 
   */
  function Upgradeable() public {
    allowedToUpgrade = true;
  }

  /**
   * @dev signals that new upgrade is available, contract must be most recent 
   * upgrade and allowed to upgrade
   * @param newContract Address of upgraded contract 
   */
  function upgradeTo(Upgradeable newContract) public ownerOnly{
    require(allowedToUpgrade && !isOldVersion);
    nextContract = newContract;
    isOldVersion = true;
    newContract.confirmUpgrade();   
  }

  /**
   * @dev confirmation that this is indeed the next version,
   * called from previous version of contract. Anyone can call this function,
   * which basically makes this instance unusable if that happens. Once called,
   * this contract can not serve as upgrade to another contract. Not an ideal solution
   * but will work until we have a more sophisticated approach using a dispatcher or similar
   */
  function confirmUpgrade() public {
    require(lastContract == address(0));
    lastContract = msg.sender;
  }
}
/**
 * @title Upgradeable Conract
 * @dev contract that implements doubly linked list to keep track of old and new 
 * versions of this contract
 */
function upgradeTo(Upgradeable newContract) public ownerOnly{
  require(allowedToUpgrade && !isOldVersion);
  nextContract = newContract;
  isOldVersion = true;
  newContract.confirmUpgrade();   
}

/**
 * @dev signals that new upgrade is available, contract must be most recent 
 * upgrade and allowed to upgrade
 * @param newContract Address of upgraded contract 
 */