Scale the indices properly

This takes into account the actual range of indices and scales them
back to fit in a max depth instead of stacking them all up at the end.