added 3stan's fix for secodnary sorted index