calculate indices correctly
calculate indices correctly