fixed a bug in getIdx
generalize it a little bit