Added back z calculation into depth sorting, though it's still not quite right.