Fix sorting and make it configurable the amount of levels of nesting allowed