// File: Administratable.sol



pragma solidity ^0.4.24;

import './Ownable.sol';

contract Administratable is Ownable {
  mapping (address => bool) admins;

  modifier onlyAdmin() {
    require(msg.sender == owner || admins[msg.sender]);
    _;
  }
  
  function addAdmin(address _adminAddr) onlyAdmin public returns (bool success) {
    admins[_adminAddr] = true;
    emit AdminAdded(_adminAddr, msg.sender);
    return true;
  }

  function revokeAdmin(address _adminAddr) onlyAdmin public returns (bool success) {
    require(msg.sender != _adminAddr);
    admins[_adminAddr] = false;
    emit AdminRevoked(_adminAddr, msg.sender);
    return true;
  }
  
  event AdminAdded(address indexed _admin, address indexed _by);
  event AdminRevoked(address indexed _admin, address indexed _by);
}

// File: BasicToken.sol



pragma solidity ^0.4.23;


import "./ERC20Basic.sol";
import "./SafeMath.sol";


/**
 * @title Basic token
 * @dev Basic version of StandardToken, with no allowances.
 */
contract BasicToken is ERC20Basic {
  using SafeMath for uint256;

  mapping(address => uint256) balances;

  uint256 totalSupply_;

  /**
  * @dev total number of tokens in existence
  */
  function totalSupply() public view returns (uint256) {
    return totalSupply_;
  }

  /**
  * @dev transfer token for a specified address
  * @param _to The address to transfer to.
  * @param _value The amount to be transferred.
  */
  function transfer(address _to, uint256 _value) public returns (bool) {
    require(_to != address(0));
    require(_value <= balances[msg.sender]);

    balances[msg.sender] = balances[msg.sender].sub(_value);
    balances[_to] = balances[_to].add(_value);
    emit Transfer(msg.sender, _to, _value);
    return true;
  }

  /**
  * @dev Gets the balance of the specified address.
  * @param _owner The address to query the the balance of.
  * @return An uint256 representing the amount owned by the passed address.
  */
  function balanceOf(address _owner) public view returns (uint256) {
    return balances[_owner];
  }

}

// File: DividendToken.sol



pragma solidity ^0.4.24;

import './Administratable.sol';
import './Pausable.sol';
import './StandardToken.sol';
import './SafeMath.sol';

contract DividendToken is StandardToken, Pausable, Administratable {
  using SafeMath for uint256;

  uint256 public period = 0;
  uint256 public buyBackTime;
  bool public ended = false;
  
  mapping (uint256 => uint256) public dividends;
  mapping (uint256 => uint256) public dividendDates;
  uint256 public buyBackTotal;
  mapping (address => bool) public boughtBack;
  
  mapping (address => mapping (uint256 => uint256)) internal holdings;
  mapping (address => uint256) internal last;
  mapping (address => uint256) public claimedTo;
  mapping (address => bool) beenDivLocked;
  mapping (address => uint256[]) divLocks;

  mapping(uint256 => uint256) totalAt;

  modifier canBuyBack() {
    require(now > buyBackTime);
    _;
  }

  modifier onlyLive() {
    require(!ended);
    _;
  }

  function updateHoldings(address _holder) internal returns (bool success) {
    uint256 lastPeriod = last[_holder];
    uint256 lastAmount = holdings[_holder][lastPeriod];
    if(lastAmount != 0) {
      for (uint i = lastPeriod + 1; i <= period; i++) {
        holdings[_holder][i] = lastAmount;
      }
    }
    last[_holder] = period;
    return true;
  }

  function updateHoldingsTo(address _holder, uint256 _to) public onlyAdmin returns (bool success){
    require(_to > last[_holder]);
    require(_to <= period);
    uint256 lastPeriod = last[_holder];
    uint256 lastAmount = holdings[_holder][lastPeriod];
    if(lastAmount != 0) {
      for (uint i = lastPeriod + 1; i <= _to; i++) {
        holdings[_holder][i] = lastAmount;
      }
    }
    last[_holder] = _to;
    return true;
  }
  
  function lockedAt(address _address, uint256 _period) public view returns (bool) {
    if(!beenDivLocked[_address]) {
      return false;
    }
    bool locked = false;
    for(uint i = 0; i < divLocks[_address].length; i++) {
      if(divLocks[_address][i] > _period) {
        break;
      } 
      locked = !locked;
    }
    return locked;
  }

  function addLock(address _locked) onlyOwner public returns (bool success) {
    require(!lockedAt(_locked, period));
    if (last[_locked] < period) {
      updateHoldings(_locked);
    }
    totalAt[period] = totalAt[period].sub(balanceOf(_locked));
    beenDivLocked[_locked] = true;
    divLocks[_locked].push(period);
    emit Locked(_locked, period);
    return true;
  }

  function revokeLock(address _unlocked) onlyOwner public returns (bool success) {
    require(lockedAt(_unlocked, period));
    if (last[_unlocked] < period) {
      updateHoldings(_unlocked);
    }
    totalAt[period] = totalAt[period].add(balanceOf(_unlocked));
    divLocks[_unlocked].push(period);
    emit Unlocked(_unlocked, period);
    return true;
  }

  function balanceOf(address _owner) public view returns (uint256 balance) {
    if(ended) {
      return 0;
    }
    return holdings[_owner][last[_owner]];
  }

  function transfer(address _to, uint256 _value) onlyLive whenNotPaused public returns (bool) {
    require(_to != address(0));
    uint256 senderLastPeriod = last[msg.sender];
    require(_value <= holdings[msg.sender][senderLastPeriod]);

    if (senderLastPeriod < period) {
      updateHoldings(msg.sender);
    }

    if (last[_to] < period) {
      updateHoldings(_to);
    }

    holdings[msg.sender][period] = holdings[msg.sender][period].sub(_value);
    holdings[_to][period] = holdings[_to][period].add(_value);
    bool fromLocked = lockedAt(msg.sender, period);
    bool toLocked = lockedAt(_to, period);
    if(fromLocked && !toLocked) {
      totalAt[period] = totalAt[period].add(_value);
    } else if(!fromLocked && toLocked) {
      totalAt[period] = totalAt[period].sub(_value);
    }
    emit Transfer(msg.sender, _to, _value);

    return true;
  }

  function transferFrom(address _from, address _to, uint256 _value) onlyLive whenNotPaused public returns (bool) {
    require(_to != address(0));
    uint256 senderLastPeriod = last[_from];
    require(_value <= holdings[_from][senderLastPeriod]);
    require(_value <= allowed[_from][msg.sender]);

     if (senderLastPeriod < period) {
       updateHoldings(_from);
    }

    if (last[_to] < period) {
      updateHoldings(_to);
    }

    holdings[_from][period] = holdings[_from][period].sub(_value);
    holdings[_to][period] = holdings[_to][period].add(_value);
    allowed[_from][msg.sender] = allowed[_from][msg.sender].sub(_value);
    bool fromLocked = lockedAt(_from, period);
    bool toLocked = lockedAt(_to, period);
    if(fromLocked && !toLocked) {
      totalAt[period] = totalAt[period].add(_value);
    } else if(!fromLocked && toLocked) {
      totalAt[period] = totalAt[period].sub(_value);
    }
    emit Transfer(_from, _to, _value);

    return true;
  }

  function () public onlyAdmin onlyLive payable {
    payIn();
  }

  function payIn() public onlyLive onlyAdmin payable returns (bool) {
    dividends[period] = msg.value;
    dividendDates[period] = now;
    period = period.add(1);
    totalAt[period] = totalAt[period.sub(1)];
    emit Paid(msg.sender, period.sub(1), msg.value);
    return true;
  }
  
  function claimDividends() whenNotPaused public returns (uint256 amount) {
    require(claimedTo[msg.sender] < period);
    uint256 total = 0;
    if (last[msg.sender] < period) {
      updateHoldings(msg.sender);
    }
    for (uint i = claimedTo[msg.sender]; i < period; i++) {
      if (holdings[msg.sender][i] > 0 && !lockedAt(msg.sender, i)) {
        uint256 multiplier = dividends[i].mul(holdings[msg.sender][i]);
        total += multiplier.div(totalAt[i]);
      }
    }
    claimedTo[msg.sender] = period;
    if(total > 0) {
      msg.sender.transfer(total);
      emit Claimed(msg.sender, i, total);
    }
    return total;
  }

  function claimDividendsFor(address _address) onlyAdmin public returns (uint256 amount) {
    require(claimedTo[_address] < period);
    uint256 total = 0;
    if (last[_address] < period) {
      updateHoldings(_address);
    }
    for (uint i = claimedTo[_address]; i < period; i++) {
      if (holdings[_address][i] > 0 && !lockedAt(_address, i)) {
        uint256 multiplier = dividends[i].mul(holdings[_address][i]);
        total += multiplier.div(totalAt[i]);
      }
    }
    claimedTo[_address] = period;
    if(total > 0) {
      _address.transfer(total);
      emit Claimed(_address, i, total);
    }
    return total;
  }
  
  function outstandingFor(address _address) public view returns (uint256 amount) {
    uint256 total = 0;
    uint256 holds = 0;
    for (uint i = claimedTo[_address]; i < period; i++) {
      if(last[_address] < i) {
        holds = holdings[_address][last[_address]];
      } else {
        holds = holdings[_address][i];
      }
      if (holds > 0 && !lockedAt(_address, i)) {
        uint256 multiplier = dividends[i].mul(holds);
        uint256 owed = multiplier.div(totalAt[i]);
        total += owed;
      }
    }
    return total;
  }

  function outstanding() public view returns (uint256 amount) {
    uint256 total = 0;
    uint256 holds = 0;
    for (uint i = claimedTo[msg.sender]; i < period; i++) {
       if(last[msg.sender] < i) {
        holds = holdings[msg.sender][last[msg.sender]];
      } else {
        holds = holdings[msg.sender][i];
      }
      if (holds > 0 && !lockedAt(msg.sender, i)) {
        uint256 multiplier = dividends[i].mul(holds);
        uint256 owed = multiplier.div(totalAt[i]);
        total += owed;
      }      
    }
    return total;
  }
  
  function buyBack() public onlyAdmin onlyLive canBuyBack payable returns (bool) {
    buyBackTotal = msg.value;
    period += 1;
    emit Paid(msg.sender, period - 1, msg.value);
    ended = true;
  }

  function claimBuyBack() public returns (bool) {
    require(ended);
    require(!boughtBack[msg.sender]);
    if (last[msg.sender] < period) {
      updateHoldings(msg.sender);
    }
    uint256 multiplier = buyBackTotal.mul(holdings[msg.sender][period]);    
    uint256 owed = multiplier.div(totalAt[period.sub(1)]);
    boughtBack[msg.sender] = true;
    msg.sender.transfer(owed);
  }

  function claimBuyBackFor(address _address) onlyAdmin public returns (bool) {
    require(ended);
    require(!boughtBack[_address]);
    if (last[_address] < period) {
      updateHoldings(_address);
    }
    uint256 multiplier = buyBackTotal.mul(holdings[_address][period]);    
    uint256 owed = multiplier.div(totalAt[period.sub(1)]);
    boughtBack[_address] = true;
    _address.transfer(owed);
  }

  function dividendDateHistory() public view returns (uint256[]) {
    uint256[] memory dates = new uint[](period);
    for(uint i = 0; i < period; i++) {
      dates[i] = dividendDates[i];
    }
    return dates;
  }

  function dividendHistory() public view returns (uint256[]) {
    uint256[] memory divs = new uint[](period);
    for(uint i = 0; i < period; i++) {
      divs[i] = dividends[i];
    }
    return divs;
  }

  function dividendHistoryFor(address _address) public view returns (uint256[]) {
    uint256[] memory divs = new uint[](period);
    for(uint i = 0; i < period; i++) {
      uint256 multiplier;
      if(last[_address] < i) {
        multiplier = dividends[i].mul(holdings[_address][i]);
      } else {
        multiplier = dividends[i].mul(holdings[_address][last[_address]]);
      }
      if(lockedAt(_address, i)) {
        divs[i] = 0;
      } else {
        divs[i] = multiplier.div(totalAt[i]);
      }
    }
    return divs;
  }

  event Paid(address indexed _sender, uint256 indexed _period, uint256 amount);

  event Claimed(address indexed _recipient, uint256 indexed _period, uint256 _amount);

  event Locked(address indexed _locked, uint256 indexed _at);

  event Unlocked(address indexed _unlocked, uint256 indexed _at);
}

// File: DividendTokenStore.sol



pragma solidity ^0.4.24;

import './Pausable.sol';

contract DividendTokenStore is Pausable {

  function totalSupply() public view returns (uint256);

  function addLock(address _locked) public returns (bool);

  function revokeLock(address _unlocked) public returns (bool);
  
  function balanceOf(address _owner) public view returns (uint256);

  function transfer(address _from, address _to, uint256 _value) public returns (bool);
  
  function () public payable {
    payIn();
  }

  function payIn() public payable returns (bool);
  
  function claimDividends() public returns (uint256);
  
  function claimDividendsFor(address _address) public returns (uint256);
    
  function buyBack() public payable returns (bool);

  function claimBuyBack() public returns (bool);

  function claimBuyBackFor(address _address) public returns (bool);

  function mint(address _to, uint256 _amount) public returns (bool);
  
  event Paid(address indexed _sender, uint256 indexed _period, uint256 amount);

  event Claimed(address indexed _recipient, uint256 indexed _period, uint256 _amount);

  event Locked(address indexed _locked, uint256 indexed _at);

  event Unlocked(address indexed _unlocked, uint256 indexed _at);
}

// File: ERC20.sol



pragma solidity ^0.4.23;

import "./ERC20Basic.sol";


/**
 * @title ERC20 interface
 * @dev see https://github.com/ethereum/EIPs/issues/20
 */
contract ERC20 is ERC20Basic {
  function allowance(address owner, address spender)
    public view returns (uint256);

  function transferFrom(address from, address to, uint256 value)
    public returns (bool);

  function approve(address spender, uint256 value) public returns (bool);
  event Approval(
    address indexed owner,
    address indexed spender,
    uint256 value
  );
}

// File: ERC20Basic.sol



pragma solidity ^0.4.23;


/**
 * @title ERC20Basic
 * @dev Simpler version of ERC20 interface
 * @dev see https://github.com/ethereum/EIPs/issues/179
 */
contract ERC20Basic {
  function totalSupply() public view returns (uint256);
  function balanceOf(address who) public view returns (uint256);
  function transfer(address to, uint256 value) public returns (bool);
  event Transfer(address indexed from, address indexed to, uint256 value);
}

// File: MoriaToken.sol



pragma solidity ^0.4.24;

import './DividendTokenStore.sol';
import './Administratable.sol';
import './StandardToken.sol';

contract MoriaToken is StandardToken, Administratable {
  
  string public constant name = "MoriaToken";
  string public constant symbol = "MOR";
  uint8 public constant decimals = 18;

  DividendTokenStore public store;
  bool public canDestroy = true;
  bool public minting = true;

  modifier isDestroyable() {
    require(canDestroy);
    _;
  }

  modifier canMint() {
    require(minting);
    _;
  }

  constructor() public {
  }

  function () public payable {
    require(store.payIn.value(msg.value)());
  }

  function totalSupply() public view returns (uint256) {
    return store.totalSupply();
  }

  function balanceOf(address _owner) public view returns (uint256 balance) {
    return store.balanceOf(_owner);
  }

  function transfer(address _to, uint256 _value) public returns (bool) {
    store.transfer(msg.sender, _to, _value);
    emit Transfer(msg.sender, _to, _value);
    return true;
  }

  function transferFrom(address _from, address _to, uint256 _value) public returns (bool) {
    require(_value <= allowed[_from][msg.sender]);
    store.transfer(_from, _to, _value);
    allowed[_from][msg.sender] = allowed[_from][msg.sender].sub(_value);
    emit Transfer(_from, _to, _value);
    return true;
  }

  function pause() public onlyOwner {
    store.pause();
  }

  function unpause() public onlyOwner {
    store.unpause();
  }

  function addLock(address _address) onlyAdmin public returns (bool) {
    return store.addLock(_address);
  }

  function revokeLock(address _address) onlyAdmin public returns (bool) {
    return store.revokeLock(_address);
  }

  function claimDividends() public returns (uint256 amount) {
    return store.claimDividendsFor(msg.sender);
  }

  function claimDividendsFor(address _address) public onlyAdmin returns (uint256 amount) {
    return store.claimDividendsFor(_address);
  }

  function buyBack() public onlyAdmin payable returns (bool) {
    require(store.buyBack.value(msg.value)());
    return true;
  }

  function claimBuyBack() public returns (bool) {
    return claimBuyBackFor(msg.sender);
  }

  function claimBuyBackFor(address _address) public onlyAdmin returns (bool) {
    return claimBuyBackFor(_address);
  }
 
  // admin

  function mint(address _from, address _to, uint256 _amount) public onlyOwner canMint returns (bool) {
    store.mint(_to, _amount);
    emit Transfer(_from, _to, _amount);
  }

  function endMinting() public onlyOwner canMint returns (bool) {
    minting = false;
  }

  function upgradeEvent(address _from, address _to) public onlyAdmin {
    emit Transfer(_from, _to,  store.balanceOf(_to));
  }

  function changeStore(DividendTokenStore _store) public onlyOwner returns (bool) {
    store = _store;
    emit StoreChanged(address(store));
    return true;
  }

  function transferStoreOwnership() public onlyOwner {
    store.transferOwnership(owner);
  }

  function destroyToken() public onlyOwner isDestroyable {
    transferStoreOwnership();
    selfdestruct(owner);
  }

  function disableSelfDestruct() public onlyOwner isDestroyable {
    canDestroy = false;
  } 

  event StoreChanged(address indexed _newStore);
  
}

// File: Ownable.sol



pragma solidity ^0.4.23;


/**
 * @title Ownable
 * @dev The Ownable contract has an owner address, and provides basic authorization control
 * functions, this simplifies the implementation of "user permissions".
 */
contract Ownable {
  address public owner;


  event OwnershipRenounced(address indexed previousOwner);
  event OwnershipTransferred(
    address indexed previousOwner,
    address indexed newOwner
  );


  /**
   * @dev The Ownable constructor sets the original `owner` of the contract to the sender
   * account.
   */
  constructor() public {
    owner = msg.sender;
  }

  /**
   * @dev Throws if called by any account other than the owner.
   */
  modifier onlyOwner() {
    require(msg.sender == owner);
    _;
  }

  /**
   * @dev Allows the current owner to relinquish control of the contract.
   */
  function renounceOwnership() public onlyOwner {
    emit OwnershipRenounced(owner);
    owner = address(0);
  }

  /**
   * @dev Allows the current owner to transfer control of the contract to a newOwner.
   * @param _newOwner The address to transfer ownership to.
   */
  function transferOwnership(address _newOwner) public onlyOwner {
    _transferOwnership(_newOwner);
  }

  /**
   * @dev Transfers control of the contract to a newOwner.
   * @param _newOwner The address to transfer ownership to.
   */
  function _transferOwnership(address _newOwner) internal {
    require(_newOwner != address(0));
    emit OwnershipTransferred(owner, _newOwner);
    owner = _newOwner;
  }
}

// File: Pausable.sol



pragma solidity ^0.4.23;


import "./Ownable.sol";


/**
 * @title Pausable
 * @dev Base contract which allows children to implement an emergency stop mechanism.
 */
contract Pausable is Ownable {
  event Pause();
  event Unpause();

  bool public paused = false;


  /**
   * @dev Modifier to make a function callable only when the contract is not paused.
   */
  modifier whenNotPaused() {
    require(!paused);
    _;
  }

  /**
   * @dev Modifier to make a function callable only when the contract is paused.
   */
  modifier whenPaused() {
    require(paused);
    _;
  }

  /**
   * @dev called by the owner to pause, triggers stopped state
   */
  function pause() onlyOwner whenNotPaused public {
    paused = true;
    emit Pause();
  }

  /**
   * @dev called by the owner to unpause, returns to normal state
   */
  function unpause() onlyOwner whenPaused public {
    paused = false;
    emit Unpause();
  }
}

// File: SafeMath.sol



pragma solidity ^0.4.23;


/**
 * @title SafeMath
 * @dev Math operations with safety checks that throw on error
 */
library SafeMath {

  /**
  * @dev Multiplies two numbers, throws on overflow.
  */
  function mul(uint256 a, uint256 b) internal pure returns (uint256 c) {
    // Gas optimization: this is cheaper than asserting 'a' not being zero, but the
    // benefit is lost if 'b' is also tested.
    // See: https://github.com/OpenZeppelin/openzeppelin-solidity/pull/522
    if (a == 0) {
      return 0;
    }

    c = a * b;
    assert(c / a == b);
    return c;
  }

  /**
  * @dev Integer division of two numbers, truncating the quotient.
  */
  function div(uint256 a, uint256 b) internal pure returns (uint256) {
    // assert(b > 0); // Solidity automatically throws when dividing by 0
    // uint256 c = a / b;
    // assert(a == b * c + a % b); // There is no case in which this doesn't hold
    return a / b;
  }

  /**
  * @dev Subtracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend).
  */
  function sub(uint256 a, uint256 b) internal pure returns (uint256) {
    assert(b <= a);
    return a - b;
  }

  /**
  * @dev Adds two numbers, throws on overflow.
  */
  function add(uint256 a, uint256 b) internal pure returns (uint256 c) {
    c = a + b;
    assert(c >= a);
    return c;
  }
}

// File: StandardDividendTokenStore.sol



pragma solidity ^0.4.24;

import './DividendTokenStore.sol';
import './SafeMath.sol';

contract StandardDividendTokenStore is DividendTokenStore {
  using SafeMath for uint256;

  uint256 public period = 0;
  uint256 public buyBackTime;
  bool public ended = false;
  
  mapping (uint256 => uint256) public dividends;
  mapping (uint256 => uint256) public dividendDates;
  uint256 public buyBackTotal;
  mapping (address => bool) public boughtBack;
  
  mapping (address => mapping (uint256 => uint256)) public holdings;
  mapping (address => uint256) public last;
  mapping (address => uint256) public claimedTo;
  mapping (address => bool) beenDivLocked;
  mapping (address => uint256[]) divLocks;

  mapping(uint256 => uint256) totalAt;
  uint256 public totalSupply_ = 0;

  modifier canBuyBack() {
    require(now > buyBackTime);
    _;
  }

  modifier onlyLive() {
    require(!ended);
    _;
  }

  constructor(uint256 _buyBackTime) public {
    buyBackTime = _buyBackTime;
  }

  function mint(address _to, uint256 _amount) public onlyOwner returns (bool) {
    require(period == 0);
    holdings[_to][0] = holdings[_to][0].add(_amount);
    totalAt[0] = totalAt[0].add(_amount);
    totalSupply_ = totalAt[0];
    return true;
  }

  function totalSupply() public view returns (uint256) {
    return totalSupply_;
  }

  function updateHoldings(address _holder) internal returns (bool success) {
    uint256 lastPeriod = last[_holder];
    uint256 lastAmount = holdings[_holder][lastPeriod];
    if(lastAmount != 0) {
      for (uint i = lastPeriod + 1; i <= period; i++) {
        holdings[_holder][i] = lastAmount;
      }
    }
    last[_holder] = period;
    return true;
  }

  function updateHoldingsTo(address _holder, uint256 _to) public whenNotPaused returns (bool success){
    require(_to > last[_holder]);
    require(_to <= period);
    uint256 lastPeriod = last[_holder];
    uint256 lastAmount = holdings[_holder][lastPeriod];
    if(lastAmount != 0) {
      for (uint i = lastPeriod + 1; i <= _to; i++) {
        holdings[_holder][i] = lastAmount;
      }
    }
    last[_holder] = _to;
    return true;
  }
  
  function lockedAt(address _address, uint256 _period) public view returns (bool) {
    if(!beenDivLocked[_address]) {
      return false;
    }
    bool locked = false;
    for(uint i = 0; i < divLocks[_address].length; i++) {
      if(divLocks[_address][i] > _period) {
        break;
      } 
      locked = !locked;
    }
    return locked;
  }

  function addLock(address _locked) onlyOwner public returns (bool success) {
    require(!lockedAt(_locked, period));
    if (last[_locked] < period) {
      updateHoldings(_locked);
    }
    totalAt[period] = totalAt[period].sub(balanceOf(_locked));
    beenDivLocked[_locked] = true;
    divLocks[_locked].push(period);
    emit Locked(_locked, period);
    return true;
  }

  function revokeLock(address _unlocked) onlyOwner public returns (bool success) {
    require(lockedAt(_unlocked, period));
    if (last[_unlocked] < period) {
      updateHoldings(_unlocked);
    }
    totalAt[period] = totalAt[period].add(balanceOf(_unlocked));
    divLocks[_unlocked].push(period);
    emit Unlocked(_unlocked, period);
    return true;
  }

  function balanceOf(address _owner) public view returns (uint256 balance) {
    if(ended) {
      return 0;
    }
    return holdings[_owner][last[_owner]];
  }

  function transfer(address _from, address _to, uint256 _value) onlyLive onlyOwner whenNotPaused public returns (bool) {
    require(_to != address(0));
    uint256 senderLastPeriod = last[_from];
    require(_value <= holdings[_from][senderLastPeriod]);

    if (senderLastPeriod < period) {
      updateHoldings(_from);
    }

    if (last[_to] < period) {
      updateHoldings(_to);
    }

    holdings[_from][period] = holdings[_from][period].sub(_value);
    holdings[_to][period] = holdings[_to][period].add(_value);
    bool fromLocked = lockedAt(_from, period);
    bool toLocked = lockedAt(_to, period);
    if(fromLocked && !toLocked) {
      totalAt[period] = totalAt[period].add(_value);
    } else if(!fromLocked && toLocked) {
      totalAt[period] = totalAt[period].sub(_value);
    }

    return true;
  }

  function () public payable {
    payIn();
  }

  function payIn() public payable onlyLive onlyOwner whenNotPaused returns (bool) {
    dividends[period] = msg.value;
    dividendDates[period] = now;
    period = period.add(1);
    totalAt[period] = totalAt[period.sub(1)];
    emit Paid(msg.sender, period.sub(1), msg.value);
    return true;
  }
  
  function claimDividends() whenNotPaused public returns (uint256 amount) {
    require(claimedTo[msg.sender] < period);
    uint256 total = 0;
    if (last[msg.sender] < period) {
      updateHoldings(msg.sender);
    }
    for (uint i = claimedTo[msg.sender]; i < period; i++) {
      if (holdings[msg.sender][i] > 0 && !lockedAt(msg.sender, i)) {
        uint256 multiplier = dividends[i].mul(holdings[msg.sender][i]);
        total += multiplier.div(totalAt[i]);
      }
    }
    claimedTo[msg.sender] = period;
    if(total > 0) {
      msg.sender.transfer(total);
      emit Claimed(msg.sender, i, total);
    }
    return total;
  }

  function claimDividendsFor(address _address) onlyOwner public returns (uint256 amount) {
    require(claimedTo[_address] < period);
    uint256 total = 0;
    if (last[_address] < period) {
      updateHoldings(_address);
    }
    for (uint i = claimedTo[_address]; i < period; i++) {
      if (holdings[_address][i] > 0 && !lockedAt(_address, i)) {
        uint256 multiplier = dividends[i].mul(holdings[_address][i]);
        total += multiplier.div(totalAt[i]);
      }
    }
    claimedTo[_address] = period;
    if(total > 0) {
      _address.transfer(total);
      emit Claimed(_address, i, total);
    }
    return total;
  }
  
  function outstandingFor(address _address) public view returns (uint256 amount) {
    uint256 total = 0;
    uint256 holds = 0;
    for (uint i = claimedTo[_address]; i < period; i++) {
      if(last[_address] < i) {
        holds = holdings[_address][last[_address]];
      } else {
        holds = holdings[_address][i];
      }
      if (holds > 0 && !lockedAt(_address, i)) {
        uint256 multiplier = dividends[i].mul(holds);
        uint256 owed = multiplier.div(totalAt[i]);
        total += owed;
      }
    }
    return total;
  }

  function outstanding() public view returns (uint256 amount) {
    uint256 total = 0;
    uint256 holds = 0;
    for (uint i = claimedTo[msg.sender]; i < period; i++) {
       if(last[msg.sender] < i) {
        holds = holdings[msg.sender][last[msg.sender]];
      } else {
        holds = holdings[msg.sender][i];
      }
      if (holds > 0 && !lockedAt(msg.sender, i)) {
        uint256 multiplier = dividends[i].mul(holds);
        uint256 owed = multiplier.div(totalAt[i]);
        total += owed;
      }      
    }
    return total;
  }
  
  function buyBack() public onlyOwner onlyLive canBuyBack payable returns (bool) {
    buyBackTotal = msg.value;
    period += 1;
    emit Paid(msg.sender, period - 1, msg.value);
    ended = true;
  }

  function claimBuyBack() public returns (bool) {
    require(ended);
    require(!boughtBack[msg.sender]);
    if (last[msg.sender] < period) {
      updateHoldings(msg.sender);
    }
    uint256 multiplier = buyBackTotal.mul(holdings[msg.sender][period]);    
    uint256 owed = multiplier.div(totalAt[period.sub(1)]);
    boughtBack[msg.sender] = true;
    msg.sender.transfer(owed);
  }

  function claimBuyBackFor(address _address) onlyOwner public returns (bool) {
    require(ended);
    require(!boughtBack[_address]);
    if (last[_address] < period) {
      updateHoldings(_address);
    }
    uint256 multiplier = buyBackTotal.mul(holdings[_address][period]);    
    uint256 owed = multiplier.div(totalAt[period.sub(1)]);
    boughtBack[_address] = true;
    _address.transfer(owed);
  }

  function dividendDateHistory() public view returns (uint256[]) {
    uint256[] memory dates = new uint[](period);
    for(uint i = 0; i < period; i++) {
      dates[i] = dividendDates[i];
    }
    return dates;
  }

  function dividendHistory() public view returns (uint256[]) {
    uint256[] memory divs = new uint[](period);
    for(uint i = 0; i < period; i++) {
      divs[i] = dividends[i];
    }
    return divs;
  }

  function dividendHistoryFor(address _address) public view returns (uint256[]) {
    uint256[] memory divs = new uint[](period);
    for(uint i = 0; i < period; i++) {
      uint256 multiplier;
      if(last[_address] < i) {
        multiplier = dividends[i].mul(holdings[_address][i]);
      } else {
        multiplier = dividends[i].mul(holdings[_address][last[_address]]);
      }
      if(lockedAt(_address, i)) {
        divs[i] = 0;
      } else {
        divs[i] = multiplier.div(totalAt[i]);
      }
    }
    return divs;
  }

  event Paid(address indexed _sender, uint256 indexed _period, uint256 amount);

  event Claimed(address indexed _recipient, uint256 indexed _period, uint256 _amount);

  event Locked(address indexed _locked, uint256 indexed _at);

  event Unlocked(address indexed _unlocked, uint256 indexed _at);
}

// File: StandardToken.sol



pragma solidity ^0.4.23;

import "./BasicToken.sol";
import "./ERC20.sol";


/**
 * @title Standard ERC20 token
 *
 * @dev Implementation of the basic standard token.
 * @dev https://github.com/ethereum/EIPs/issues/20
 * @dev Based on code by FirstBlood: https://github.com/Firstbloodio/token/blob/master/smart_contract/FirstBloodToken.sol
 */
contract StandardToken is ERC20, BasicToken {

  mapping (address => mapping (address => uint256)) internal allowed;


  /**
   * @dev Transfer tokens from one address to another
   * @param _from address The address which you want to send tokens from
   * @param _to address The address which you want to transfer to
   * @param _value uint256 the amount of tokens to be transferred
   */
  function transferFrom(
    address _from,
    address _to,
    uint256 _value
  )
    public
    returns (bool)
  {
    require(_to != address(0));
    require(_value <= balances[_from]);
    require(_value <= allowed[_from][msg.sender]);

    balances[_from] = balances[_from].sub(_value);
    balances[_to] = balances[_to].add(_value);
    allowed[_from][msg.sender] = allowed[_from][msg.sender].sub(_value);
    emit Transfer(_from, _to, _value);
    return true;
  }

  /**
   * @dev Approve the passed address to spend the specified amount of tokens on behalf of msg.sender.
   *
   * Beware that changing an allowance with this method brings the risk that someone may use both the old
   * and the new allowance by unfortunate transaction ordering. One possible solution to mitigate this
   * race condition is to first reduce the spender's allowance to 0 and set the desired value afterwards:
   * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
   * @param _spender The address which will spend the funds.
   * @param _value The amount of tokens to be spent.
   */
  function approve(address _spender, uint256 _value) public returns (bool) {
    allowed[msg.sender][_spender] = _value;
    emit Approval(msg.sender, _spender, _value);
    return true;
  }

  /**
   * @dev Function to check the amount of tokens that an owner allowed to a spender.
   * @param _owner address The address which owns the funds.
   * @param _spender address The address which will spend the funds.
   * @return A uint256 specifying the amount of tokens still available for the spender.
   */
  function allowance(
    address _owner,
    address _spender
   )
    public
    view
    returns (uint256)
  {
    return allowed[_owner][_spender];
  }

  /**
   * @dev Increase the amount of tokens that an owner allowed to a spender.
   *
   * approve should be called when allowed[_spender] == 0. To increment
   * allowed value is better to use this function to avoid 2 calls (and wait until
   * the first transaction is mined)
   * From MonolithDAO Token.sol
   * @param _spender The address which will spend the funds.
   * @param _addedValue The amount of tokens to increase the allowance by.
   */
  function increaseApproval(
    address _spender,
    uint _addedValue
  )
    public
    returns (bool)
  {
    allowed[msg.sender][_spender] = (
      allowed[msg.sender][_spender].add(_addedValue));
    emit Approval(msg.sender, _spender, allowed[msg.sender][_spender]);
    return true;
  }

  /**
   * @dev Decrease the amount of tokens that an owner allowed to a spender.
   *
   * approve should be called when allowed[_spender] == 0. To decrement
   * allowed value is better to use this function to avoid 2 calls (and wait until
   * the first transaction is mined)
   * From MonolithDAO Token.sol
   * @param _spender The address which will spend the funds.
   * @param _subtractedValue The amount of tokens to decrease the allowance by.
   */
  function decreaseApproval(
    address _spender,
    uint _subtractedValue
  )
    public
    returns (bool)
  {
    uint oldValue = allowed[msg.sender][_spender];
    if (_subtractedValue > oldValue) {
      allowed[msg.sender][_spender] = 0;
    } else {
      allowed[msg.sender][_spender] = oldValue.sub(_subtractedValue);
    }
    emit Approval(msg.sender, _spender, allowed[msg.sender][_spender]);
    return true;
  }

}