Fixing indexing and sort