abstract contract NFTOptimized is Context, ERC165, IERC721, IERC721Metadata, IERC721Enumerable {
    using Address for address;

    string private _name;
    string private _symbol;
    uint256 private burnCount;

    address[] internal _owners;
    mapping(uint256 => address) private _tokenApprovals;
    mapping(address => mapping(address => bool)) private _operatorApprovals;

    constructor(string memory name_, string memory symbol_) {
        _name = name_;
        _symbol = symbol_;
    }     

    function approve(address to, uint256 tokenId) public virtual override {
        address owner = NFTOptimized.ownerOf(tokenId);
        require(to != owner, "Approval for Owner is not necessary");
        require(_msgSender() == owner || isApprovedForAll(owner, _msgSender()), "Approve function call by non-approved caller");

        _approve(to, tokenId);
    }

    function setApprovalForAll(address operator, bool approved) public virtual override {
        require(operator != _msgSender(), "Approval for Owner is not necessary");
        _operatorApprovals[_msgSender()][operator] = approved;
        emit ApprovalForAll(_msgSender(), operator, approved);
    }

    function transferFrom(address from, address to, uint256 tokenId) public virtual override {
        require(_isApprovedOrOwner(_msgSender(), tokenId), "Transfer function call by non-approved caller");
        _transfer(from, to, tokenId);
    }

    function safeTransferFrom(address from, address to, uint256 tokenId) public virtual override {
        safeTransferFrom(from, to, tokenId, "");
    }

    function safeTransferFrom(address from, address to, uint256 tokenId, bytes memory _data) public virtual override {
        require(_isApprovedOrOwner(_msgSender(), tokenId), "safeTransfer function call by non-approved caller");
        _safeTransfer(from, to, tokenId, _data);
    }

    function _safeTransfer(address from, address to, uint256 tokenId, bytes memory _data) internal virtual {
        _transfer(from, to, tokenId);
        require(_checkOnERC721Received(from, to, tokenId, _data), "Error from ERC721Receiver check");
    }

	function _safeMint(address to, uint256 tokenId) internal virtual {
        _safeMint(to, tokenId, "");
    }
	function _safeMint(address to, uint256 tokenId, bytes memory _data) internal virtual {
        _mint(to, tokenId);
        require(_checkOnERC721Received(address(0), to, tokenId, _data), "Error from ERC721Receiver check");
    }
	function _mint(address to, uint256 tokenId) internal virtual {
        require(!_exists(tokenId), "Token already exists");
        require(to != address(0), "Cannot mint to Null Address");
        
        _beforeTokenTransfer(address(0), to, tokenId);
        _owners.push(to);

        emit Transfer(address(0), to, tokenId);
    }

	function _burn(uint256 tokenId) internal virtual {
        address owner = NFTOptimized.ownerOf(tokenId);

        _beforeTokenTransfer(owner, address(0), tokenId);
        _approve(address(0), tokenId);
        _owners[tokenId] = address(0);
        burnCount++;

        emit Transfer(owner, address(0), tokenId);
    }

	function _transfer(address from, address to, uint256 tokenId) internal virtual {
        require(NFTOptimized.ownerOf(tokenId) == from, "Caller does not own token");
        require(to != address(0), "Cannot transfer to Null Address");

        _beforeTokenTransfer(from, to, tokenId);
        _approve(address(0), tokenId);
        _owners[tokenId] = to;

        emit Transfer(from, to, tokenId);
    }

	function _approve(address to, uint256 tokenId) internal virtual {
        _tokenApprovals[tokenId] = to;
        emit Approval(NFTOptimized.ownerOf(tokenId), to, tokenId);
    }

	function _checkOnERC721Received(address from, address to, uint256 tokenId, bytes memory _data) private returns (bool) {
        if (to.isContract()) {
            try IERC721Receiver(to).onERC721Received(_msgSender(), from, tokenId, _data) returns (bytes4 retval) {
                return retval == IERC721Receiver.onERC721Received.selector;
            } catch (bytes memory reason) {
                if (reason.length == 0) {
                    revert("ERC721: transfer to non ERC721Receiver implementer");
                } else {
                    assembly {
                        revert(add(32, reason), mload(reason))
                    }
                }
            }
        } else {
            return true;
        }
    }
    //Leaving this in incase anyone needs to overwrite this function for additional functionality
	function _beforeTokenTransfer(address from, address to, uint256 tokenId) internal virtual {}

    /**
    VIEW FUNCTIONS
     */
     
    function supportsInterface(bytes4 interfaceId) public view virtual override(ERC165, IERC165) returns (bool) {
        return interfaceId == type(IERC721).interfaceId || interfaceId == type(IERC721Metadata).interfaceId || interfaceId == type(IERC721Enumerable).interfaceId || super.supportsInterface(interfaceId);
    }

    function balanceOf(address owner) public view virtual override returns (uint256) {
        require(owner != address(0), "Cannot measure balance of Null Address");
        uint count = 0;
        uint length = _owners.length;
        for(uint i = 0; i < length; i++){
          if(owner == _owners[i] ){
            count++;
          }
        }
        delete length;
        return count;
    }

    function ownerOf(uint256 tokenId) public view virtual override returns (address) {
        address owner = _owners[tokenId];
        require(owner != address(0), "Checking for Owner of nonexistent tokenid");
        return owner;
    }

    function name() public view virtual override returns (string memory) {
        return _name;
    }

    function symbol() public view virtual override returns (string memory) {
        return _symbol;
    }

    function getApproved(uint256 tokenId) public view virtual override returns (address) {
        require(_exists(tokenId), "Checking for Owner of nonexistent tokenid");
        return _tokenApprovals[tokenId];
    }

    function isApprovedForAll(address owner, address operator) public view virtual override returns (bool) {
        return _operatorApprovals[owner][operator];
    }

    function _exists(uint256 tokenId) internal view virtual returns (bool) {
        return tokenId < _owners.length && _owners[tokenId] != address(0);
    }

    function _isApprovedOrOwner(address spender, uint256 tokenId) internal view virtual returns (bool) {
        require(_exists(tokenId), "Checking for Owner of nonexistent tokenid");
        address owner = NFTOptimized.ownerOf(tokenId);
        return (spender == owner || getApproved(tokenId) == spender || isApprovedForAll(owner, spender));
    }

    function totalSupply() public view virtual override returns (uint256) {
        return _owners.length - burnCount;
    }

    function totalCreated() public view virtual returns (uint256) {
        return _owners.length;
    }

    function totalBurned() public view virtual returns (uint256) {
        return burnCount;
    }

    function tokensOfOwner(address owner) public view returns (uint256[] memory) {
        require(0 < NFTOptimized.balanceOf(owner), "Address owns no tokens");
        uint256 tokenCount = balanceOf(owner);
        uint256[] memory tokenIds = new uint256[](tokenCount);

        for (uint256 i = 0; i < tokenCount; i++) {
            tokenIds[i] = tokenOfOwnerByIndex(owner, i);
        }
        return tokenIds;
    }

    function tokenOfOwnerByIndex(address owner, uint256 index) public view override returns (uint256 tokenId) {
        require(index < NFTOptimized.balanceOf(owner), "Token does not exist");
        uint count;

        for( uint i; i < _owners.length; ++i ){
            if( owner == _owners[i] ){
                if( count == index )
                    return i;
                else
                    ++count;
            }
        }
        require(false, "Token does not exist");
    }

    function tokenByIndex(uint256 index) public view virtual override returns (uint256) {
        require(index < NFTOptimized.totalCreated(), "Token does not exist");
        return index;
    }

}
function _beforeTokenTransfer(address from, address to, uint256 tokenId) internal virtual {}

//Leaving this in incase anyone needs to overwrite this function for additional functionality