More performant implementation of the ballIndexes function