Added a depth sorting function.