contract BasicCoinManager is Owned {
	// a structure wrapping a deployed BasicCoin
	struct Coin {
		address coin;
		address owner;
		address tokenreg;
	}

	// a new BasicCoin has been deployed
	event Created(address indexed owner, address indexed tokenreg, address indexed coin);

	// a list of all the deployed BasicCoins
	Coin[] coins;

	// all BasicCoins for a specific owner
	mapping (address => uint[]) ownedCoins;

	// the base, tokens denoted in micros (matches up with BasicCoin interface above)
	uint constant public base = 1000000;

	// return the number of deployed
	function count() public view returns (uint) {
		return coins.length;
	}

	// get a specific deployment
	function get(uint _index) public view returns (address coin, address owner, address tokenreg) {
		Coin memory c = coins[_index];

		coin = c.coin;
		owner = c.owner;
		tokenreg = c.tokenreg;
	}

	// returns the number of coins for a specific owner
	function countByOwner(address _owner) public view returns (uint) {
		return ownedCoins[_owner].length;
	}

	// returns a specific index by owner
	function getByOwner(address _owner, uint _index) public view returns (address coin, address owner, address tokenreg) {
		return get(ownedCoins[_owner][_index]);
	}

	// deploy a new BasicCoin on the blockchain
	function deploy(uint _totalSupply, string memory _tla, string memory _name, address _tokenreg) public payable returns (bool) {
		TokenReg tokenreg = TokenReg(_tokenreg);
		BasicCoin coin = new BasicCoin(_totalSupply, msg.sender);

		uint ownerCount = countByOwner(msg.sender);
		uint fee = tokenreg.fee();

		ownedCoins[msg.sender].length = ownerCount + 1;
		ownedCoins[msg.sender][ownerCount] = coins.length;
		coins.push(Coin(address(coin), msg.sender, address(tokenreg)));
		tokenreg.registerAs.value(fee)(address(coin), _tla, base, _name, msg.sender);

		emit Created(msg.sender, address(tokenreg), address(coin));

		return true;
	}

	// owner can withdraw all collected funds
	function drain() public only_owner {
		if (!msg.sender.send(address(this).balance)) {
			revert();
		}
	}
}
// Manages BasicCoin instances, including the deployment & registration
function count() public view returns (uint) {
	return coins.length;
}

// return the number of deployed