Refactor array index computation