Calculate index correctly