@Override
  public void updateEntity() {
    if (!formed || pos != 17) return;

    if (!worldObj.isRemote) {
      boolean update = false;
      int prevAmount = tank2.getFluidAmount();
      boolean enabled;
      if (computerControlled) enabled = computerOn;
      else
        enabled =
            !worldObj.isBlockIndirectlyGettingPowered(
                xCoord + (facing == 4 ? -1 : facing == 5 ? 1 : facing == 2 ? -2 : 2),
                yCoord + 1,
                zCoord + (facing == 2 ? -1 : facing == 3 ? 1 : facing == 4 ? 2 : -2));
      if (enabled) {
        RefineryRecipe recipe = getRecipe(true);
        if (recipe != null) {
          int consumed = Config.getInt("refinery_consumption");
          if (energyStorage.extractEnergy(consumed, true) == consumed
              && tank2.fill(recipe.output.copy(), false) == recipe.output.amount) {
            int drain0 =
                tank0.getFluid().isFluidEqual(recipe.input0)
                    ? recipe.input0.amount
                    : recipe.input1.amount;
            int drain1 =
                tank0.getFluid().isFluidEqual(recipe.input0)
                    ? recipe.input1.amount
                    : recipe.input0.amount;
            if (tank0.getFluidAmount() >= drain0 && tank1.getFluidAmount() >= drain1) {
              energyStorage.extractEnergy(consumed, false);
              tank0.drain(drain0, true);
              tank1.drain(drain1, true);
              tank2.fill(recipe.output.copy(), true);
              update = true;
            }
          }
        }
      }
      if (tank2.getFluidAmount() > 0) {
        ItemStack filledContainer = Utils.fillFluidContainer(tank2, inventory[4], inventory[5]);
        if (filledContainer != null) {
          if (inventory[5] != null
              && OreDictionary.itemMatches(inventory[5], filledContainer, true))
            inventory[5].stackSize += filledContainer.stackSize;
          else if (inventory[5] == null) inventory[5] = filledContainer.copy();
          this.decrStackSize(4, filledContainer.stackSize);
          update = true;
        }
        if (tank2.getFluidAmount() > 0) {
          ForgeDirection f = ForgeDirection.getOrientation(facing);
          int out = Math.min(144, tank2.getFluidAmount());
          TileEntity te =
              Utils.getExistingTileEntity(
                  worldObj, xCoord + f.offsetX * 2, yCoord, zCoord + f.offsetZ * 2);
          if (te instanceof IFluidHandler
              && ((IFluidHandler) te).canFill(f.getOpposite(), tank2.getFluid().getFluid())) {
            int accepted =
                ((IFluidHandler) te)
                    .fill(f.getOpposite(), new FluidStack(tank2.getFluid().getFluid(), out), false);
            FluidStack drained = this.tank2.drain(accepted, true);
            ((IFluidHandler) te).fill(f.getOpposite(), drained, true);
          }
        }
      }
      if (tank2.getFluidAmount() != prevAmount) update = true;

      ItemStack emptyContainer = Utils.drainFluidContainer(tank0, inventory[0]);
      if (emptyContainer != null) {
        if (inventory[1] != null && OreDictionary.itemMatches(inventory[1], emptyContainer, true))
          inventory[1].stackSize += emptyContainer.stackSize;
        else if (inventory[1] == null) inventory[1] = emptyContainer.copy();
        this.decrStackSize(0, emptyContainer.stackSize);
        update = true;
      }
      emptyContainer = Utils.drainFluidContainer(tank1, inventory[2]);
      if (emptyContainer != null) {
        if (inventory[3] != null && OreDictionary.itemMatches(inventory[3], emptyContainer, true))
          inventory[3].stackSize += emptyContainer.stackSize;
        else if (inventory[3] == null) inventory[3] = emptyContainer.copy();
        this.decrStackSize(2, emptyContainer.stackSize);
        update = true;
      }

      if (update) {
        this.markDirty();
        worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
      }
    }
  }