contract iBBTs is ERC20, Regulated, AcceptEth {
  using SafeMath for uint;

  string public symbol;
  string public  name;
  uint8 public decimals;
  uint public _totalShares;

  mapping(address => uint) balances;
  mapping(address => mapping(address => uint)) allowed;

  constructor() public {
// (c) by A. Valamontes with Blockchain Ventures / Geopay.me Inc 2013-2019. Affero GPLv3 Licence.
// ----------------------------------------------------------------------------------------------
    symbol = "iBBTs";                                             // Create the Security Token Symbol Here
    name = "iBlockchain Bank and Trust Securities/Share Token ";  // Description of the Securetized Tokens
    // In our sample we have created securites tokens and fractional securities for the tokens upto 18 digits
    decimals = 2;                                                 // Number of Digits [0-18] If an organization wants to fractionalize the securities
    // The 0 can be any digit up to 18. Eighteen is the standard for cryptocurrencies
    _totalShares = 10000000 * 10**uint(decimals);                // Total Number of Securities Issued (example 10,000,000 Shares of iBBTs)
    balances[owner] = _totalShares;
    emit Transfer(address(0), owner, _totalShares);               // Owner or Company Representative (CFO/COO/CEO/CHAIRMAN)

    regulationStatus[owner] = true;
    emit ShareholderRegistered(owner);
  }

  function issue(address recipient, uint tokens) public onlyOwner returns (bool success) {
    require(recipient != address(0));
    require(recipient != owner);
    
    RegisterShareholder(recipient);
    transfer(recipient, tokens);
    return true;
  }

  function transferOwnership(address newOwner) public onlyOwner {
    // Organization is Merged or Sold and Securities Management needs to transfer to new owners
    require(newOwner != address(0));
    require(newOwner != owner);
   
    RegisterShareholder(newOwner);
    transfer(newOwner, balances[owner]);
    owner = newOwner;
  }

  function totalSupply() public returns (uint supply) {
    return _totalShares - balances[address(0)];
  }

  function balanceOf(address tokenOwner) public returns (uint balance) {
    return balances[tokenOwner];
  }

  function transfer(address to, uint tokens) public returns (bool success) {
    ensureRegulated(msg.sender);
    ensureRegulated(to);
    
    balances[msg.sender] = balances[msg.sender].sub(tokens);
    balances[to] = balances[to].add(tokens);
    emit Transfer(msg.sender, to, tokens);
    return true;
  }

  function approve(address spender, uint tokens) public returns (bool success) {
    // Put a check for race condition issue with approval workflow of ERC20
    require((tokens == 0) || (allowed[msg.sender][spender] == 0));
    
    allowed[msg.sender][spender] = tokens;
    emit Approval(msg.sender, spender, tokens);
    return true;
  }

  function transferFrom(address from, address to, uint tokens) public returns (bool success) {
    ensureRegulated(from);
    ensureRegulated(to);

    balances[from] = balances[from].sub(tokens);
    allowed[from][msg.sender] = allowed[from][msg.sender].sub(tokens);
    balances[to] = balances[to].add(tokens);
    emit Transfer(from, to, tokens);
    return true;
  }

  function allowance(address tokenOwner, address spender) public returns (uint remaining) {
    return allowed[tokenOwner][spender];
  }

  // --------------------------------------------------------------------------------------------
  // Corporation can issue more shares or revoce/cancel shares
  // https://github.com/ethereum/EIPs/pull/621
  // --------------------------------------------------------------------------------------------
  
  // --------------------------------------------------------------------------------------------
  // Owner can transfer out any accidentally sent ERC20 tokens
  // --------------------------------------------------------------------------------------------
  function transferOtherERC20Assets(address tokenAddress, uint tokens) public onlyOwner returns (bool success) {
        return ERC20Interface(tokenAddress).transfer(owner, tokens);
  }
}
function transferOtherERC20Assets(address tokenAddress, uint tokens) public onlyOwner returns (bool success) {
      return ERC20Interface(tokenAddress).transfer(owner, tokens);
}

// --------------------------------------------------------------------------------------------
// Corporation can issue more shares or revoce/cancel shares
// https://github.com/ethereum/EIPs/pull/621
// --------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------
// Owner can transfer out any accidentally sent ERC20 tokens
// --------------------------------------------------------------------------------------------