Fixed indexing for nearest neighbors