fixed index loop bug