add boundry checks for index and period