Ender Excavate v 1.0

What is Ender Excavate? Ender Excavate is a replacement Excavate program that makes turtle excavating not quite as "Check-In" heavy. It currently works exactly like the default excavate program, with a few changes.

How it works: You need two seperate enderchests, one for fuel, and one to hold items. Place the item enderchest in the first slot, and the fuel chest in the second slot. The syntax for using this program is the same as the default, the program name, followed by a number. (i.e. EnderExcavate 10 would mine a 10 x 10 mine shaft)

Planned features:
  • Chuck Reload Friendly
  • Ability to set length, width, and depth seperately
  • Detect if item enderchest is full, wait until space is available
  • Detect if fuel chest is empty, wait for fuel
  • Ability to recieve commands over rednet
Warnings:
The current version of the code does not check if either enderchest is full/empty, if you do not keep the item chest empty, or the fuel chest full, the code will break

Known Bugs:

Please post here with any suggestions/bugs so I can improve the code, thanks!

Pastebin URL: pastebin.com/mY7f2HqC

Without further ado, here is the code:
Spoiler

args={...}
x=args[1]
y=0
j=1
turn="Right"
canMoveForward=function()
	while not turtle.forward() do
		if turtle.detect() then
			turtle.dig()
		else
			turtle.attack()
		end
	end
	j = j + 1
end
canMoveDown=function()
	while not turtle.down() do
		if turtle.detectDown() then
			turtle.digDown()
		else
			turtle.attackDown()
		end
	end
end
digLevel=function()
	for i=1, x do
		while j<tonumber(x) do
			canMoveForward()
		end
		if i<tonumber(x)
		then
			if turn=="Left"
			then
				turn="Right"
				turtle.turnLeft()
				canMoveForward()
				turtle.turnLeft()
			else
				turn="Left"
				turtle.turnRight()
				canMoveForward()
				turtle.turnRight()
			end
		else
			if turn=="Left"
			then
				turtle.turnRight()
				turtle.turnRight()
			else
				turtle.turnLeft()
				turtle.turnLeft()
			end
		end
		j=1
	end
end
print("Mining " .. x .. " x " .. x .. " mine")
while not turtle.detectDown() or turtle.digDown() do
	digLevel()
	turn="Right"
	if turtle.getItemCount(16)>0
	then
		turtle.select(1)
		turtle.placeUp()
		for i=3, 16 do
			turtle.select(i)
			turtle.dropUp()
		end
		turtle.select(1)
		turtle.digUp()
	end
	if turtle.getFuelLevel()< (x * x)
	then
		turtle.select(2)
		turtle.placeUp()
		turtle.select(16)
		turtle.refuel()
		turtle.suckUp(1,1)
		turtle.select(2)
		turtle.digUp()
	end
	canMoveDown()
	y=y+1
end
digLevel()
for i=1, y do
	turtle.up()
end
turtle.select(1)
	turtle.placeUp()
	for i=3, 16 do
		turtle.select(i)
		turtle.dropUp()
	end
turtle.select(1)
turtle.digUp()
print( x .. " x " .. x .. " mine complete")