fix index off by one bug