added index to make sure program doesn't loop forever on exit