Updated Layer Sorting

Updated the layer sorting algorithm