contract ERC721Token is ERC721, Pausable {

    /* ======== STATE VARIABLES ======== */

    /**
     * @dev A mapping from token IDs to the address that owns them.
     */
    mapping(uint => address) tokenIdToOwner;

    /**
     * @dev A mapping from token ids to an address that has been approved to call
     *  transferFrom(). Each token can only have one approved address for transfer
     *  at any time. A zero value means no approval is outstanding.
     */
    mapping (uint => address) tokenIdToApproved;

    /**
     * @dev A mapping from token ID to index of the ownerTokens' tokens list.
     */
    mapping(uint => uint) tokenIdToOwnerTokensIndex;


    /* ======== PUBLIC/EXTERNAL FUNCTIONS ======== */

    /**
     * @dev Returns the number of tokens owned by a specific address.
     * @param _owner The owner address to check.
     */
    function balanceOf(address _owner) public view returns (uint) {
        return ownerTokens[_owner].length;
    }

    /**
     * @dev Returns the address currently assigned ownership of a given token.
     */
    function ownerOf(uint _tokenId) external view returns (address) {
        require(tokenIdToOwner[_tokenId] != address(0));

        return tokenIdToOwner[_tokenId];
    }

    /**
    * @dev Returns the approved address of a given token.
    */
    function approvedFor(uint _tokenId) external view returns (address) {
        return tokenIdToApproved[_tokenId];
    }

    /**
     * @dev Get an array of IDs of each token that an user owns.
     */
    function getOwnerTokens(address _owner) external view returns(uint[]) {
        return ownerTokens[_owner];
    }

    /**
     * @dev External function to transfers a token to another address.
     * @param _to The address of the recipient, can be a user or contract.
     * @param _tokenId The ID of the token to transfer.
     */
    function transfer(address _to, uint _tokenId) whenNotPaused external {
        // Safety check to prevent against an unexpected 0x0 default.
        require(_to != address(0));

        // Disallow transfers to this contract to prevent accidental misuse.
        require(_to != address(this));

        // You can only send your own token.
        require(_owns(msg.sender, _tokenId));

        // Reassign ownership, clear pending approvals, emit Transfer event.
        _transfer(msg.sender, _to, _tokenId);
    }

    /**
     * @dev Grant another address the right to transfer a specific Kitty via
     *  transferFrom(). This is the preferred flow for transfering NFTs to contracts.
     * @param _to The address to be granted transfer approval. Pass address(0) to
     *  clear all approvals.
     * @param _tokenId The ID of the Kitty that can be transferred if this call succeeds.
     */
    function approve(address _to, uint _tokenId) whenNotPaused external {
        // Only an owner can grant transfer approval.
        require(_owns(msg.sender, _tokenId));

        // Register the approval (replacing any previous approval).
        _approve(_tokenId, _to);

        // Emit approval event.
        Approval(msg.sender, _to, _tokenId);
    }

    /**
     * @dev Transfer a Kitty owned by another address, for which the calling address
     *  has previously been granted transfer approval by the owner.
     * @param _from The address that owns the Kitty to be transfered.
     * @param _to The address that should take ownership of the Kitty. Can be any address,
     *  including the caller.
     * @param _tokenId The ID of the Kitty to be transferred.
     */
    function transferFrom(address _from, address _to, uint _tokenId) whenNotPaused external {
        // Safety check to prevent against an unexpected 0x0 default.
        require(_to != address(0));

        // Check for approval and valid ownership
        require(tokenIdToApproved[_tokenId] == msg.sender);
        require(_owns(_from, _tokenId));

        // Reassign ownership (also clears pending approvals and emits Transfer event).
        _transfer(_from, _to, _tokenId);
    }


    /* ======== INTERNAL/PRIVATE FUNCTIONS ======== */

    /**
     * @dev Assigns ownership of a specific token to an address.
     */
    function _transfer(address _from, address _to, uint _tokenId) internal {
        // Step 1: Remove token from _form address.
        // When creating new token, _from is 0x0.
        if (_from != address(0)) {
            uint[] storage fromTokens = ownerTokens[_from];
            uint tokenIndex = tokenIdToOwnerTokensIndex[_tokenId];

            // Put the last token to the transferred token index and update its index in ownerTokensIndexes.
            uint lastTokenId = fromTokens[fromTokens.length - 1];

            // Do nothing if the transferring token is the last item.
            if (_tokenId != lastTokenId) {
                fromTokens[tokenIndex] = lastTokenId;
                tokenIdToOwnerTokensIndex[lastTokenId] = tokenIndex;
            }

            fromTokens.length--;
        }

        // Step 2: Add token to _to address.
        // Transfer ownership.
        tokenIdToOwner[_tokenId] = _to;

        // Add the _tokenId to ownerTokens[_to] and remember the index in ownerTokensIndexes.
        tokenIdToOwnerTokensIndex[_tokenId] = ownerTokens[_to].length;
        ownerTokens[_to].push(_tokenId);

        // Emit the Transfer event.
        Transfer(_from, _to, _tokenId);
    }

    /**
     * @dev Marks an address as being approved for transferFrom(), overwriting any previous
     *  approval. Setting _approved to address(0) clears all transfer approval.
     */
    function _approve(uint _tokenId, address _approved) internal {
        tokenIdToApproved[_tokenId] = _approved;
    }


    /* ======== MODIFIERS ======== */

    /**
     * @dev Throws if _dungeonId is not created yet.
     */
    modifier tokenExists(uint _tokenId) {
        require(_tokenId < totalSupply());
        _;
    }

    /**
     * @dev Checks if a given address is the current owner of a particular token.
     * @param _claimant The address we are validating against.
     * @param _tokenId Token ID
     */
    function _owns(address _claimant, uint _tokenId) internal view returns (bool) {
        return tokenIdToOwner[_tokenId] == _claimant;
    }

}
/**
 * @title The ERC-721 compliance token contract.
 */
function _approve(uint _tokenId, address _approved) internal {
    tokenIdToApproved[_tokenId] = _approved;
}

/**
 * @dev Marks an address as being approved for transferFrom(), overwriting any previous
 *  approval. Setting _approved to address(0) clears all transfer approval.
 */