Added a stack layering system

It works by trying to work out the dependencies of the stacks and
ordering them so that stacks with dependencies happen after those stacks