Added error check to avoid out of bounds error.