Change algorithm for finding array indices from coordinates