}
 
        /* Allocate and initialize mtd_info, nand_chip and nandsim structures */
-       nsmtd = kzalloc(sizeof(struct mtd_info) + sizeof(struct nand_chip)
-                               + sizeof(struct nandsim), GFP_KERNEL);
-       if (!nsmtd) {
+       chip = kzalloc(sizeof(struct nand_chip) + sizeof(struct nandsim),
+                      GFP_KERNEL);
+       if (!chip) {
                NS_ERR("unable to allocate core structures.\n");
                return -ENOMEM;
        }
-       chip        = (struct nand_chip *)(nsmtd + 1);
+       nsmtd       = nand_to_mtd(chip);
         nsmtd->priv = (void *)chip;
        nand        = (struct nandsim *)(chip + 1);
        chip->priv  = (void *)nand;
        for (i = 0;i < ARRAY_SIZE(nand->partitions); ++i)
                kfree(nand->partitions[i].name);
 error:
-       kfree(nsmtd);
+       kfree(chip);
        free_lists();
 
        return retval;
        nand_release(nsmtd); /* Unregister driver */
        for (i = 0;i < ARRAY_SIZE(ns->partitions); ++i)
                kfree(ns->partitions[i].name);
-       kfree(nsmtd);        /* Free other structures */
+       kfree(mtd_to_nand(nsmtd));        /* Free other structures */
        free_lists();
 }